This is the mail archive of the
cygwin-apps@cygwin.com
mailing list for the Cygwin project.
Re: Integrating Ralf's rebase into setup.exe
- From: Charles Wilson <cwilson at ece dot gatech dot edu>
- To: Ralf Habacker <Ralf dot Habacker at freenet dot de>
- Cc: cygwin-apps at cygwin dot com
- Date: Thu, 02 Jan 2003 13:55:50 -0500
- Subject: Re: Integrating Ralf's rebase into setup.exe
- References: <007801c2b28a$e279cfa0$31a707d5@BRAMSCHE>
Ralf Habacker wrote:
You have gotten me just between finishing the things. I was interrupted a little
after written the last mail, so 10 minutes later is was mostly fixed.
Lucky me. <g>
5) is the 'bindimage.cc' file used at all?
it is used in rebind_main.cc.
You mean it *should* be used in rebind_main.cc. It's not listed in
Makefile.in as one of the dependencies for REBIND_FILES, nor is it a
member of LIBFILES.
Of course, rebind_main doesn't call BindImage or BindImageEx. It does
have a commented-out call to dll.rebind() -- but
LinkedObjectFile::rebind() doesn't call BindImage() or BindImageEx
either -- LinkedObjectFile::rebind() does all of the work itself.
Looks like the factorization isn't quite complete...work in progress, right?
Anyway, here's my remaining patches. With two exceptions (Makefile and
rebase_main.cc (main)), these are basically removing all printf calls
and stdio.h, in favor of cout, cerr, and stream manipulators.
Anything that was printf() became std::cout, fprintf(stderr,) became
cerr. However, there are still some "design" issues that I didn't touch:
1) some that go to stdout probably should be stderr, and vice versa.
2) some are guarded by 'if (debug)' and probably should be 'if (some
library call that returns the value of debug for this instance)' -- but
I didn't really check the lexical scope. These may be fine.
3) some aren't even guarded at all, and should be.
4) Then there's that whole exceptions vs. print-an-error-message from
within the library deal.
The point is, ALL console output is now C++, cout/cerr style (before, it
was half C++, half printf)
--Chuck
2002-01-02 Charles Wilson <cwilson@ece.gatech.edu>
* Makefile.in: add bindimage.o to LIBFILES
change ar flags for creating static library
* bindimage.cc: include imagehelper.h
* objectfile.cc: remove dependency on stdio.h, use
iostream and iomanip instead.
(ObjectFile::ObjectFile): use std::cout instead of
printf, std::cerr instead of fprintf(stderr,...)
(LinkedObjectFile::rebind): ditto.
(LinkedObjectFile:LinkedObjectFile): ditto.
(LinkedObjectFile::PrintDependencie): ditto.
(LinkedObjectFile::unbind): ditto.
(main): ditto.
* objectfilelist.cc: remove dependency on stdio.h, use
iostream instead.
(main): use std::cout instead of
printf, std::cerr instead of fprintf(stderr,...)
* rebase_main.cc: remove dependency on stdio.h
(main): don't call GetLastError() twice, use stored
value in aStatus2. Call strerror() for a more informative
message.
* rebind_main.cc: remove dependency on stdio.h
(Usage): use std::cout instead of printf, std::cerr
instead of fprintf(stderr,...)
* sections.cc: remove dependency on stdio.h, use
iostream and iomanip instead.
(Section::print): use std::cout instead of printf, std::cerr
instead of fprintf(stderr,...)
(Exports::Exports): ditto.
(Exports::dump): ditto.
(Imports::Imports): ditto.
(Imports::dump): ditto.
(Relocations::check): ditto.
(Relocations::fix): ditto.
(Relocations::relocate): ditto.
* unbind_main (Usage): ditto.
Index: Makefile.in
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/Makefile.in,v
retrieving revision 1.1
diff -u -r1.1 Makefile.in
--- Makefile.in 2 Jan 2003 18:14:32 -0000 1.1
+++ Makefile.in 2 Jan 2003 18:35:56 -0000
@@ -9,7 +9,8 @@
LIB_TARGET=imagehelper
LIB_TARGET_FILE=libimagehelper.a
LIB_FILES=objectfile.o objectfilelist.o sections.o debug.o \
- rebaseimage.o checkimage.o fiximage.o getimageinfos.o
+ rebaseimage.o checkimage.o fiximage.o getimageinfos.o \
+ bindimage.o
#
# applications
@@ -33,7 +34,7 @@
$(LIB_TARGET): $(LIB_TARGET_FILE)
$(LIB_TARGET_FILE): $(LIB_FILES)
- ar -qf $@ $^
+ ar -cru $@ $^
$(REBASE_TARGET): $(REBASE_FILES)
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)
Index: bindimage.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/bindimage.cc,v
retrieving revision 1.5
diff -u -r1.5 bindimage.cc
--- bindimage.cc 1 Jan 2003 11:55:49 -0000 1.5
+++ bindimage.cc 2 Jan 2003 18:35:57 -0000
@@ -21,6 +21,7 @@
#include <iostream>
#include <sstream>
+#include "imagehelper.h"
#include "objectfile.h"
BOOL BindImageEx(
Index: objectfile.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/objectfile.cc,v
retrieving revision 1.6
diff -u -r1.6 objectfile.cc
--- objectfile.cc 2 Jan 2003 17:05:14 -0000 1.6
+++ objectfile.cc 2 Jan 2003 18:35:59 -0000
@@ -18,8 +18,9 @@
* $Id: objectfile.cc,v 1.6 2003/01/02 17:05:14 habacker Exp $
*/
-#include <stdio.h>
#include <stdlib.h>
+#include <iostream>
+#include <iomanip>
#include <time.h>
#include "objectfile.h"
@@ -78,7 +79,7 @@
strcat(name,"/");
strcat(name,basename);
if (debug)
- printf("%s: name: %s\n", __FUNCTION__, name);
+ std::cout << __FUNCTION__ << ": name:" << name << std::endl;
hfile = CreateFile(Win32Path(name), writeable ? GENERIC_READ | GENERIC_WRITE : GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
// found
if (hfile != INVALID_HANDLE_VALUE)
@@ -159,8 +160,10 @@
if (debug)
{
- fprintf(stderr,"Base: 0x%08x\n",lpFileBase);
- fprintf(stderr,"ImageBase: 0x%08x\n",ImageBase);
+ std::cerr << "Base: 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << lpFileBase << std::dec << std::endl;
+ std::cerr << "ImageBase: 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << ImageBase << std::dec << std::endl;
}
Section *edata = sections->find(".edata");
@@ -186,7 +189,7 @@
{
if (!isWritable)
{
- printf("%s: error - the objectfile is not writeable\n",__FUNCTION__);
+ std::cout << __FUNCTION__ << ": error - the objectfile is not writeable" << std::endl;
return false;
}
// FIXME: set error code
@@ -206,26 +209,27 @@
bool autoImportFlag;
int *patch_address;
char *dllname = (char *)p->Name + idata->getAdjust();
- // fprintf(stderr,"%s\n",dllname);
+ // std::cerr << dllname << std::endl;
if (!(obj = (LinkedObjectFile *)cache.get(dllname) ) )
{
obj = new LinkedObjectFile(dllname);
if (obj->getError())
{
- fprintf(stderr,"cant load dll '%s'\n",dllname);
+ std::cerr << "cant load dll '" << dllname << "'" << std::endl;
delete obj;
continue;
}
cache.add(obj);
}
- fprintf(stderr,"%s\n",obj->getFileName());
+ std::cerr << obj->getFileName() << std::endl;
PIMAGE_THUNK_DATA hintArray = PIMAGE_THUNK_DATA ((uint) p->OriginalFirstThunk + imports->getAdjust());
PIMAGE_THUNK_DATA firstArray;
if (debug)
- fprintf(stderr,"FirstThunk %08x\n",p->FirstThunk);
+ std::cerr << "FirstThunk 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << p->FirstThunk << std::dec << std::endl;
if ((autoImportFlag = text->isIn((uint)p->FirstThunk)))
firstArray = PIMAGE_THUNK_DATA ((uint) p->FirstThunk + text->getAdjust());
@@ -233,14 +237,15 @@
firstArray = PIMAGE_THUNK_DATA ((uint) p->FirstThunk + imports->getAdjust());
if (debug)
- fprintf(stderr,"FirstArray %08x\n",firstArray);
+ std::cerr << "FirstArray 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << firstArray << std::dec << std::endl;
for (; hintArray->u1.Function; hintArray++, firstArray++)
{
PIMAGE_IMPORT_BY_NAME a = PIMAGE_IMPORT_BY_NAME ((uint)hintArray->u1.AddressOfData + idata->getAdjust());
if (debug)
- fprintf(stderr,"symbol: %s\n",a->Name);
+ std::cerr << "symbol: " << a->Name << std::endl;
if (autoImportFlag)
patch_address = (int *)&firstArray;
@@ -248,13 +253,18 @@
patch_address = (int *)&firstArray->u1.Function;
if (debug)
- fprintf(stderr,"patch_address %08x content %08x\n",patch_address, *patch_address);
+ std::cerr << "patch_address 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << patch_address << std::dec << \
+ " content 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << *patch_address << std::dec << \
+ std::endl;
char *name = (char *)a->Name;
uint addr = obj->exports->getVirtualAddress(name);
if (debug)
- fprintf(stderr,"symaddr: %08x\n",addr + obj->ImageBase);
+ std::cerr << "symaddr: 0x" << std::setw(8) << std::setfill('0') \
+ << std::hex << addr + obj->ImageBase << std::dec << std::endl;
*patch_address = (addr + obj->ImageBase);
}
// set
@@ -310,9 +320,9 @@
if (!isPrinted)
{
if (level == 0)
- fprintf(stderr,"%s:\n",getFileName());
+ std::cerr << getFileName() << std::endl;
else
- fprintf(stderr,"%s%s\n",filler + strlen(filler)-level*2, getFileName());
+ std::cerr << filler + strlen(filler)-level*2 << getFileName() << std::endl;
}
else
{
@@ -332,7 +342,7 @@
obj = new LinkedObjectFile(dllname);
if (obj->getError())
{
- fprintf(stderr,"cant load dll '%s'\n",dllname);
+ std::cerr << "cant load dll '" << dllname << "'" << std::endl;
delete obj;
continue;
}
@@ -354,7 +364,7 @@
{
if (!isWritable)
{
- printf("%s: error - the objectfile is not writeable\n",__FUNCTION__);
+ std::cout << __FUNCTION__ << ": error - the objectfile is not writeable" << std::endl;
return false;
}
// FIXME: set error code
@@ -395,20 +405,26 @@
int main(int argc, char **argv)
{
ObjectFile test("/bin/cygz.dll");
- printf("%s\n",test.getFileName());
+ std::cout << test.getFileName() << std::endl;
LinkedObjectFile test("/bin/cygz.dll");
/* FIXME: update necessary
char *symbol = "gzgets";
- fprintf(stderr,"%-20s %08x\n",symbol,test.getSymbolAddress(symbol));
-
+ std::cerr << std::setw(20) << std::setfill(' ') << symbol << " 0x" \
+ << std::setw(8) << std::setfill('0') \
+ << std::hex << test.getSymbolAddress(symbol) << std::dec << std::endl;
+
symbol = "_dist_code";
- fprintf(stderr,"%-20s %08x\n",symbol,test.getSymbolAddress(symbol));
+ std::cerr << std::setw(20) << std::setfill(' ') << symbol << " 0x" \
+ << std::setw(8) << std::setfill('0') \
+ << std::hex << test.getSymbolAddress(symbol) << std::dec << std::endl;
symbol = "zlibVersion";
- fprintf(stderr,"%-20s %08x\n",symbol,test.getSymbolAddress(symbol));
+ std::cerr << std::setw(20) << std::setfill(' ') << symbol << " 0x" \
+ << std::setw(8) << std::setfill('0') \
+ << std::hex << test.getSymbolAddress(symbol) << std::dec << std::endl;
*/
return 0;
Index: objectfilelist.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/objectfilelist.cc,v
retrieving revision 1.2
diff -u -r1.2 objectfilelist.cc
--- objectfilelist.cc 5 Dec 2002 15:56:29 -0000 1.2
+++ objectfilelist.cc 2 Jan 2003 18:35:59 -0000
@@ -18,8 +18,8 @@
* $Id: objectfilelist.cc,v 1.2 2002/12/05 15:56:29 habacker Exp $
*/
-#include <stdio.h>
#include <stdlib.h>
+#include <iostream>
#include "objectfilelist.h"
@@ -83,7 +83,7 @@
test.Add("/bin/cygz.dll");
ObjectFile *dll = test.getObjectFile("/bin/cygz.dll");
if (dll)
- printf("%s\n",dll->getFileName());
+ std::cout << dll->getFileName() << std::endl;
return 0;
}
Index: rebase_main.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/rebase_main.cc,v
retrieving revision 1.5
diff -u -r1.5 rebase_main.cc
--- rebase_main.cc 2 Jan 2003 17:05:14 -0000 1.5
+++ rebase_main.cc 2 Jan 2003 18:36:00 -0000
@@ -29,7 +29,6 @@
#include <sys/cygwin.h>
#endif
#include <windows.h>
-#include <stdio.h>
#include "imagehelper.h"
@@ -103,7 +102,7 @@
if (aStatus2 != 0)
{
cerr << "ReBaseImage(" << aFile.c_str() <<"," << hex << aNewImageBase <<") failed with last error = " <<
- dec << GetLastError() << endl;
+ dec << aStatus2 << ": " << strerror(aStatus2) << endl;
exit(2);
}
cout << aFile << hex << ": new base = " <<
Index: rebind_main.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/rebind_main.cc,v
retrieving revision 1.7
diff -u -r1.7 rebind_main.cc
--- rebind_main.cc 2 Jan 2003 17:05:14 -0000 1.7
+++ rebind_main.cc 2 Jan 2003 18:36:00 -0000
@@ -18,7 +18,6 @@
* $Id: rebind_main.cc,v 1.7 2003/01/02 17:05:14 habacker Exp $
*/
-#include <stdio.h>
#include <stdlib.h>
#include <iostream>
@@ -76,6 +75,6 @@
void
Usage()
{
- printf("usage: rebind <dll path>\n");
+ cout << "usage: rebind <dll path>" << endl;
}
Index: sections.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/sections.cc,v
retrieving revision 1.3
diff -u -r1.3 sections.cc
--- sections.cc 5 Dec 2002 15:56:29 -0000 1.3
+++ sections.cc 2 Jan 2003 18:36:02 -0000
@@ -18,7 +18,8 @@
* $Id: sections.cc,v 1.3 2002/12/05 15:56:29 habacker Exp $
*/
-#include <stdio.h>
+#include <iostream>
+#include <iomanip>
#include "sections.h"
@@ -34,8 +35,12 @@
void Section::print(char *title)
{
- fprintf(stderr,"%-10s name: %-8s: base: %08x size: %08x file offset: %08x offset: %08x\n",
- title,Name,header->VirtualAddress,header->SizeOfRawData,header->PointerToRawData, adjust);
+ std::cerr << std::setw(10) << std::setfill(' ') << title \
+ << " name: " << std::setw(8) << std::setfill(' ') << Name \
+ << " base: 0x" << std::setw(8) << std::setfill('0') << std::hex << header->VirtualAddress << std::dec \
+ << " size: 0x" << std::setw(8) << std::setfill('0') << std::hex << header->SizeOfRawData << std::dec \
+ << " file offset: 0x" << std::setw(8) << std::setfill('0') << std::hex << header->PointerToRawData << std::dec \
+ << " offset: 0x" << std::setw(8) << std::setfill('0') << std::hex << adjust << std::dec << std::endl;
}
bool Section::isIn(Section &in)
@@ -112,7 +117,7 @@
{
exports = 0;
adjust = 0;
- // fprintf(stderr,"%s - error: can't find section for creating export object",__FUNCTION__);
+ // std::cerr << __FUNCTION__ << " - error: can't find section for creating export object" << std::endl;
}
}
@@ -161,15 +166,15 @@
{
char *p;
- printf("exports\n");
+ std::cout << "exports" << std::endl;
if (!exports)
- printf("\tno exports available\n");
+ std::cout << "\tno exports available" << std::endl;
else
{
reset();
while (p = getNext())
{
- printf("\t%s\n",p);
+ std::cout << "\t" << p << std::endl;
}
}
}
@@ -195,7 +200,7 @@
{
imports = 0;
adjust = 0;
- // fprintf(stderr,"%s - error: can't find section for creating import object\n",__FUNCTION__);
+ // std::cerr << __FUNCTION__ << " - error: can't find section for creating import object" << std::endl;
}
}
@@ -216,23 +221,23 @@
{
PIMAGE_IMPORT_DESCRIPTOR p;
- printf("imports\n");
+ std::cout << "imports" << std::endl;
reset();
while (p = getNextDescriptor())
{
- printf("%s \n",p->Name + adjust);
- printf("vma: Hint Time Forward DLL First\n");
- printf(" Table Stamp Chain Name Thunk\n");
- printf("%08x %08x %08x %08x %08x %08x\n",
- 0,
- p->OriginalFirstThunk,
- p->TimeDateStamp,
- p->ForwarderChain,
- p->Name,
- p->FirstThunk);
+ std::cout << p->Name + adjust << std::endl;
+ std::cout << "vma: Hint Time Forward DLL First" << std::endl;
+ std::cout << " Table Stamp Chain Name Thunk" << std::endl;
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << 0 << std::dec << " ";
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << p->OriginalFirstThunk << std::dec << " ";
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << p->TimeDateStamp << std::dec << " ";
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << p->ForwarderChain << std::dec << " ";
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << (void *)p->Name << std::dec << " ";
+ std::cout << std::setw(8) << std::setfill('0') << std::hex << p->FirstThunk << std::dec << std::endl;
+
}
- printf("\n");
+ std::cout << std::endl;
}
@@ -274,7 +279,9 @@
cursec = sections->find(va);
if (!cursec)
{
- printf("warning: dll is corrupted - relocations for '%08x' are pointing to a non existent section\n",va);
+ std::cout << "warning: dll is corrupted - relocations for '0x" \
+ << std::setw(8) << std::setfill('0') << std::hex << va << std::dec \
+ << "' are pointing to a non existent section" << std::endl;
errors++;
continue;
}
@@ -291,7 +298,7 @@
if (!relocs)
return false;
- printf("warning: fixing bad relocations .... ");
+ std::cout << "warning: fixing bad relocations .... ";
for (; (char *)relocp < (char *)relocs + size && relocp->SizeOfBlock != 0; ((char *)relocp) += relocp->SizeOfBlock )
{
@@ -309,9 +316,9 @@
}
}
if (errors == 0)
- printf("no errors found\n");
+ std::cout << "no errors found" << std::endl;
else
- printf("corrupted relocation records fixed\n");
+ std::cout << "corrupted relocation records fixed" << std::endl;
return true;
}
@@ -331,8 +338,9 @@
PWORD p = (PWORD)((unsigned int )relocp + sizeof(IMAGE_BASE_RELOCATION));
if (debug)
{
- printf("VirtAdress: %08x\n",relocp->VirtualAddress);
- printf("NumOfRelocs: %d\n",NumOfRelocs);
+ std::cout << "VirtAdress: 0x" \
+ << std::setw(8) << std::setfill('0') << std::hex << relocp->VirtualAddress << std::dec << std::endl;
+ std::cout << "NumOfRelocs: " << NumOfRelocs << std::endl;
}
WholeNumOfRelocs += NumOfRelocs;
@@ -341,7 +349,9 @@
Section *cursec = sections->find(va);
if (!cursec)
{
- printf("warning: dll is corrupted - the relocations '%08x' points to a non existing section and could not be relocated\n",va);
+ std::cout << "warning: dll is corrupted - the relocations '0x" \
+ << std::setw(8) << std::setfill('0') << std::hex << va << std::dec \
+ << "' points to a non existing section and could not be relocated" << std::endl;
continue;
}
else if (debug)
@@ -355,8 +365,12 @@
int location = (*p & 0x0fff) + va;
if (debug)
{
- fprintf(stderr,"%08x - ",location);
- fprintf(stderr,"%08x\n",location + adjust + 3);
+ std::cerr << "0x" \
+ << std::setw(8) << std::setfill('0') << std::hex << location << std::dec \
+ << " - ";
+ std::cerr << "0x" \
+ << std::setw(8) << std::setfill('0') << std::hex << location + adjust + 3 << std::dec \
+ << std::endl;
}
int *patch_adr = (int *)cursec->rva2real(location);
*patch_adr += difference;
Index: unbind_main.cc
===================================================================
RCS file: /cvsroot/kde-cygwin/tools/rebase/unbind_main.cc,v
retrieving revision 1.7
diff -u -r1.7 unbind_main.cc
--- unbind_main.cc 2 Jan 2003 17:05:14 -0000 1.7
+++ unbind_main.cc 2 Jan 2003 18:36:02 -0000
@@ -77,6 +77,6 @@
void
Usage()
{
- printf("usage: unbind <dll path>\n");
+ cout << "usage: unbind <dll path>" << endl;
}