[ros-diffs] [arty] 32591: Fix us back up to use PE-COFF again. This is getting ridiculous, but I now really understand everything, and freeldr moved the image mapping code to a common area.

arty at svn.reactos.org arty at svn.reactos.org
Sun Mar 9 12:34:58 CET 2008


Author: arty
Date: Fri Mar  7 07:53:55 2008
New Revision: 32591

URL: http://svn.reactos.org/svn/reactos?rev=3D32591&view=3Drev
Log:
Fix us back up to use PE-COFF again.  This is getting ridiculous, but I now
really understand everything, and freeldr moved the image mapping code to
a common area.

Added:
    trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/coff.h   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/exports.cpp   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/exports.h   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/imports.cpp   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/imports.h   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h   (with props)
    trunk/tools/RosBE/RosBE-PPC/elfpe/section.h   (with props)
Modified:
    trunk/tools/RosBE/RosBE-PPC/elfpe/Makefile
    trunk/tools/RosBE/RosBE-PPC/elfpe/elfpe.cpp
    trunk/tools/RosBE/RosBE-PPC/elfpe/header.cpp
    trunk/tools/RosBE/RosBE-PPC/elfpe/header.h
    trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.cpp
    trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.h
    trunk/tools/RosBE/RosBE-PPC/elfpe/util.cpp
    trunk/tools/RosBE/RosBE-PPC/elfpe/util.h

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/Makefile
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/M=
akefile?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/Makefile (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/Makefile Fri Mar  7 07:53:55 2008
@@ -1,6 +1,6 @@
 elfpe: argparse.cpp argparse.h header.cpp header.h \
 	objectfile.cpp objectfile.h util.cpp util.h pedef.h \
-	elfpe.cpp
+	elfpe.cpp reloc.cpp coff.cpp exports.cpp imports.cpp
 	g++ -g -o $@ $^ -lelf
 =

 clean:

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/c=
off.cpp?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp Fri Mar  7 07:53:55 2008
@@ -1,0 +1,42 @@
+#include "coff.h"
+
+PECoffExecutable::PECoffExecutable
+(const ElfObjectFile &elf, const std::string &file, uint32_t filealign)
+    : elf(elf), tempFile(file), filealign(filealign)
+{
+    int i;
+    FILE *f =3D fopen(tempFile.c_str(), "wb");
+    if (!f) return;
+
+    // Write PE Header
+    const ElfObjectFile::Section &section =3D *elf.getNamedSection(".pehea=
der");
+    fwrite(section.getSectionData(), 1, section.logicalSize(), f);
+    =

+    // Write out sections
+    uint32_t physSize;
+    uint32_t bytes =3D 0;
+
+    for (i =3D 1; i < elf.getNumSections(); i++)
+    {
+        const ElfObjectFile::Section &section =3D elf.getSection(i);
+        if (section.getName() =3D=3D ".peheader") continue;
+	if (section.getType() !=3D SHT_NOBITS && section.getFlags() & SHF_ALLOC)
+	{
+	    physSize =3D roundup(section.logicalSize(), filealign);
+#if 0
+	    printf("P %08x:%08x %s\n", =

+		   (int)ftell(f),
+		   section.logicalSize(), =

+		   section.getName().c_str());
+#endif
+	    bytes =3D fwrite(section.getSectionData(), 1, section.logicalSize(), =
f);
+	    while(bytes++ < physSize) fputc(0, f);
+	}
+    }
+    fclose(f);
+}
+
+void PECoffExecutable::Write(const std::string &resultFile)
+{
+    rename(tempFile.c_str(), resultFile.c_str());
+}

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/coff.cpp
---------------------------------------------------------------------------=
---
    svn:executable =3D *

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/coff.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/c=
off.h?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/coff.h (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/coff.h Fri Mar  7 07:53:55 2008
@@ -1,0 +1,22 @@
+#ifndef COMPDVR_COFF_H
+#define COMPDVR_COFF_H
+
+#include <vector>
+#include <utility>
+#include "pedef.h"
+#include "util.h"
+#include "objectfile.h"
+#include "section.h"
+
+class PECoffExecutable {
+public:
+    PECoffExecutable(const ElfObjectFile &elf, const std::string &tmpFile,=
 uint32_t filealign);
+    void Write(const std::string &resultFile);
+
+private:
+    const ElfObjectFile &elf;
+    std::string tempFile;
+    uint32_t filealign;
+};
+
+#endif//COMPDVR_COFF_H

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/coff.h
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/elfpe.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/e=
lfpe.cpp?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/elfpe.cpp (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/elfpe.cpp Fri Mar  7 07:53:55 2008
@@ -12,8 +12,13 @@
 #include "objectfile.h"
 #include "header.h"
 #include "argparse.h"
+#include "reloc.h"
+#include "coff.h"
+#include "exports.h"
+#include "imports.h"
 =

 #define TYPE_PEHEADER (SHT_LOOS + 1)
+#define TYPE_PERELOC  (SHT_LOOS + 2)
 =

 int execute_command( bool verbose, const std::vector<std::string> &args )
 {
@@ -55,6 +60,26 @@
         }
     }
     args.insert(args.begin(), the_definition + "=3D" + body);
+}
+
+std::vector<uint8_t> =

+ProcessRelocSections
+(const ElfObjectFile &eof, =

+ uint32_t imageBase,
+ const std::vector<section_mapping_t> &rvas)
+{
+    std::vector<uint8_t> relocData;
+    uint32_t relocAddr =3D (uint32_t)-1;
+    int i, j;
+    for (i =3D 1; i < eof.getNumSections(); i++)
+    {
+        const ElfObjectFile::Section &section =3D eof.getSection(i);
+        if (section.getType() =3D=3D SHT_REL ||
+            section.getType() =3D=3D SHT_RELA)
+            SingleRelocSection(eof, section, rvas, relocData, imageBase, r=
elocAddr);
+    }
+    AddReloc(relocData, 0, relocAddr, 0, 0);
+    return relocData;
 }
 =

 int main( int argc, char **argv ) {
@@ -100,6 +125,11 @@
         if(entry_point.size() && entry_point[0] =3D=3D '_')
         {
             entry_point =3D entry_point.substr(1);
+        }
+        size_t at =3D entry_point.find('@');
+        if (at !=3D std::string::npos)
+        {
+            entry_point =3D entry_point.substr(0, at);
         }
         if( !compile_only )
         {
@@ -142,7 +172,7 @@
     if( verbose ) gcc_args_str.insert(gcc_args_str.begin(),"-v");
     if (!compile_only)
     {
-    	if( is_dll ) gcc_args_str.insert(gcc_args_str.begin(), "-Wl,-r");
+    	//gcc_args_str.insert(gcc_args_str.begin(), "-Wl,-r");
         gcc_args_str.insert(gcc_args_str.begin(), "-Wl,-q");
         gcc_args_str.insert(gcc_args_str.begin(), "-Wl,-d");
         gcc_args_str.insert(gcc_args_str.begin(), "-Wl,--start-group");
@@ -160,30 +190,56 @@
     }
     if (!compile_only) gcc_args_str.insert(gcc_args_str.end(), "-Wl,--end-=
group");
 =

+    std::vector<section_mapping_t> sectionRvas;
     if ( !(status =3D execute_command( verbose, gcc_args_str )) && !compil=
e_only && mkheader )
     {
         /* Ok fixup the elf object file */
         ElfObjectFile eof(output_file);
+        if(!eof) exit(1); =

+        =

+        uint32_t imageSize;
         const ElfObjectFile::Symbol *entry_sym;
-
-        if(!eof) exit(1);
-
         entry_sym =3D eof.getNamedSymbol(entry_point);
-
+        =

+        /* This computes the section RVAs */
         ElfPeHeader header
-	(strtoul(image_base.c_str(), 0, 0),
-	 strtoul(section_align.c_str(), 0, 0),
-	 strtoul(file_align.c_str(), 0, 0),
-	 entry_sym,
-	 0x10000,
-	 0x100000,
-	 0x10000,
-	 0x100000,
-	 atoi(subsystem.c_str()),
-	 is_dll,
-	 &eof);
-
-        eof.addSection(".peheader", header.getData(), TYPE_PEHEADER);
+            (strtoul(image_base.c_str(), 0, 0),
+             strtoul(section_align.c_str(), 0, 0),
+             strtoul(file_align.c_str(), 0, 0),
+             entry_sym,
+             0x10000,
+             0x100000,
+             0x10000,
+             0x100000,
+             atoi(subsystem.c_str()),
+             is_dll,
+             &eof);
+        =

+        // Get base section layout
+        imageSize =3D header.getSectionRvas(sectionRvas);
+
+        // Add relocation info
+        std::vector<uint8_t> relocSection =3D =

+            ProcessRelocSections
+            (eof, strtoul(image_base.c_str(), 0, 0), sectionRvas);
+        eof.addSection(".reloc", relocSection, SHT_PROGBITS);
+        eof.update();
+
+        // Recompute RVAs after adding reloc section
+        imageSize =3D header.getSectionRvas(sectionRvas);
+        header.createHeaderSection(sectionRvas, imageSize);
+        eof.addSection(".peheader", header.getData(), SHT_PROGBITS);
+        eof.update();
+
+        // Fixup exports
+        ExportFixup(eof, sectionRvas);
+
+        // Fixup imports
+        ImportFixup(eof, sectionRvas);
+        eof.update();
+
+        PECoffExecutable cof(eof, output_file + ".tmp", strtoul(file_align=
.c_str(), 0, 0));
+        cof.Write(output_file);
     }
 =

     return status;

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/exports.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/e=
xports.cpp?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/exports.cpp (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/exports.cpp Fri Mar  7 07:53:55 2008
@@ -1,0 +1,46 @@
+#include "exports.h"
+#include "objectfile.h"
+#include "section.h"
+
+void ExportFixup
+(ElfObjectFile &eof,
+ const std::vector<section_mapping_t> &mapping)
+{
+    const ElfObjectFile::Section *exportSect =3D eof.getNamedSection(".eda=
ta");
+    if (!exportSect) return;
+    uint8_t *exportTable =3D exportSect->getSectionData();
+    // Fixup the words of the export directory
+    uint8_t *exportTarget =3D exportTable;
+    uint32_t exportRva =3D FindRVA(mapping, exportSect->getNumber());
+
+    uint32_t nameRva, ordinalBase, numberOfAddress, numberOfNames, address=
Rva,
+        namePtrRva, ordinalTableRva;
+    int i;
+
+    le32write_postinc(exportTarget, be32read(exportTarget));
+    le32write_postinc(exportTarget, be32read(exportTarget));
+    le16write_postinc(exportTarget, be16read(exportTarget));
+    le16write_postinc(exportTarget, be16read(exportTarget));
+    le32write_postinc(exportTarget, (nameRva =3D be32read(exportTarget)));
+    le32write_postinc(exportTarget, (ordinalBase =3D be32read(exportTarget=
)));
+    le32write_postinc(exportTarget, (numberOfAddress =3D be32read(exportTa=
rget)));
+    le32write_postinc(exportTarget, (numberOfNames =3D be32read(exportTarg=
et)));
+    le32write_postinc(exportTarget, (addressRva =3D be32read(exportTarget)=
));
+    le32write_postinc(exportTarget, (namePtrRva =3D be32read(exportTarget)=
));
+    le32write_postinc(exportTarget, (ordinalTableRva =3D be32read(exportTa=
rget)));
+    =

+    // Address Table
+    exportTarget =3D exportTable + addressRva - exportRva;
+    for (i =3D 0; i < numberOfAddress; i++)
+        le32write_postinc(exportTarget, be32read(exportTarget));
+    =

+    // Name table
+    exportTarget =3D exportTable + namePtrRva - exportRva;
+    for (i =3D 0; i < numberOfNames; i++)
+        le32write_postinc(exportTarget, be32read(exportTarget));
+
+    // Ordinal table
+    exportTarget =3D exportTable + ordinalTableRva - exportRva;
+    for (i =3D 0; i < numberOfAddress; i++)
+        le16write_postinc(exportTarget, be16read(exportTarget));
+}

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/exports.cpp
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/exports.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/e=
xports.h?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/exports.h (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/exports.h Fri Mar  7 07:53:55 2008
@@ -1,0 +1,14 @@
+#ifndef COMPDVR_EXPORTS_H
+#define COMPDVR_EXPORTS_H
+
+#include <vector>
+#include <utility>
+#include "pedef.h"
+#include "util.h"
+#include "objectfile.h"
+#include "section.h"
+
+void ExportFixup
+(ElfObjectFile &eof, const std::vector<section_mapping_t> &mapping);
+
+#endif//COMPDVR_EXPORTS_H

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/exports.h
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/header.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/h=
eader.cpp?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/header.cpp (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/header.cpp Fri Mar  7 07:53:55 2008
@@ -16,6 +16,7 @@
 	 ElfObjectFile *eof) :
 	imagebase(imagebase),
 	sectionalign(sectionalign),
+        filealign(filealign),
 	stackreserve(stackreserve),
 	stackcommit(stackcommit),
 	heapreserve(heapreserve),
@@ -25,19 +26,15 @@
 	eof(eof)
 {
     data.resize(computeSize());
-    createHeaderSection();
 }
 =

 int ElfPeHeader::computeSize() const
 {
-    return sectionalign; /* We'll compute it for real later */
-}
-
-void ElfPeHeader::createHeaderSection()
-{
-    std::vector<section_mapping_t> sectionRvaSet;
-    uint32_t imageSize =3D getSectionRvas(sectionRvaSet);
-
+    return roundup(0x80 + 0x200, filealign); /* We'll compute it for real =
later */
+}
+
+void ElfPeHeader::createHeaderSection(const std::vector<section_mapping_t>=
 &sectionRvaSet, uint32_t imageSize)
+{
     data[0] =3D 'M'; data[1] =3D 'Z';
     uint8_t *dataptr =3D &data[0x3c];
     uint32_t coffHeaderSize, optHeaderSizeMember;
@@ -88,7 +85,7 @@
     le32pwrite_postinc(dataptr, getResourceInfo(sectionRvaSet));
     le32pwrite_postinc(dataptr, getExceptionInfo());
     le32pwrite_postinc(dataptr, getSecurityInfo());
-    le32pwrite_postinc(dataptr, getRelocInfo());
+    le32pwrite_postinc(dataptr, getRelocInfo(sectionRvaSet));
     le32pwrite_postinc(dataptr, getDebugInfo());
     le32pwrite_postinc(dataptr, getDescrInfo());
     le32pwrite_postinc(dataptr, getMachInfo());
@@ -101,28 +98,37 @@
     // size, but leaving out the other info.  We write the section name
     // truncated into the name field and leave the section id in the
     // physical address bit
+
+    uint32_t paddr =3D computeSize();
     for (int i =3D 0; i < sectionRvaSet.size(); i++)
     {
 	section_mapping_t mapping =3D sectionRvaSet[i];
 	const ElfObjectFile::Section *section =3D mapping.section;
 	std::string name =3D section->getName();
 	uint32_t size =3D section->logicalSize();
+        uint32_t psize =3D =

+	    section->getType() =3D=3D SHT_NOBITS ? 0 : roundup(size, filealign);
 	uint32_t rva =3D mapping.rva;
 	for (int j =3D 0; j < 8; j++)
 	{
 	    *dataptr++ =3D j < name.size() ? name[j] : '\000';
 	}
+
+#if 0
+	printf("V %08x:%08x P %08x:%08x %s\n",
+	       rva, size, paddr, psize, name.c_str());
+#endif
+
 	le32write_postinc(dataptr, size);
 	le32write_postinc(dataptr, rva);
-	le32write_postinc(dataptr, size);
-	// Note: we put the index in the offset slot so we can find the
-	// real offset later in the loader
-	le32write_postinc(dataptr, sectionRvaSet[i].index);
+	le32write_postinc(dataptr, psize);
+	le32write_postinc(dataptr, paddr);
 	le32write_postinc(dataptr, 0);
 	le32write_postinc(dataptr, 0);
 	le32write_postinc(dataptr, 0);
         // XXX Figure out the real flags
 	le32write_postinc(dataptr, IMAGE_SCN_CNT_CODE);
+        paddr +=3D psize;
     }
 }
 =

@@ -132,14 +138,17 @@
 {
     uint32_t start =3D computeSize();
     uint32_t limit =3D start;
-    for(int i =3D 0; i < eof->getNumSections(); i++) {
+
+    rvas.clear();
+
+    for(int i =3D 1; i < eof->getNumSections(); i++) {
 	{
 	    const ElfObjectFile::Section &sect =3D eof->getSection(i);
 	    if(sect.getFlags() & SHF_ALLOC) {
 		limit =3D roundup(start + sect.logicalSize(), sectionalign);
 #if 0
-		fprintf(stderr, "rva[%02d:%s] =3D (%x %x %d)\n",
-			rvas.size(), sect.getName().c_str(), &sect, start, i);
+		fprintf(stderr, "rva[%02d:-%20s] =3D (%08x %08x %08x %d)\n",
+			rvas.size(), sect.getName().c_str(), start, sect.getStartRva(), sect.lo=
gicalSize(), i);
 #endif
 		rvas.push_back(section_mapping_t(&sect, start, i));
 	    }
@@ -198,9 +207,9 @@
     return std::make_pair(0,0);
 }
 =

-u32pair_t ElfPeHeader::getRelocInfo() const
-{
-    return std::make_pair(0,0);
+u32pair_t ElfPeHeader::getRelocInfo(const std::vector<section_mapping_t> &=
mapping) const
+{
+    return getNamedSectionInfo(eof, mapping, ".reloc");
 }
 =

 u32pair_t ElfPeHeader::getDebugInfo() const

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/header.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/h=
eader.h?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/header.h (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/header.h Fri Mar  7 07:53:55 2008
@@ -6,18 +6,7 @@
 #include "pedef.h"
 #include "util.h"
 #include "objectfile.h"
-
-typedef struct section_mapping_t {
-    const ElfObjectFile::Section *section;
-    uint32_t rva;
-    int index;
-
-    section_mapping_t
-    (const ElfObjectFile::Section *sect, uint32_t rva, int index) :
-	section(sect), rva(rva), index(index) { }
-    section_mapping_t(const section_mapping_t &other) :
-	section(other.section), rva(other.rva), index(other.index) { }
-} section_mapping_t;
+#include "section.h"
 =

 class ElfPeHeader {
 public:
@@ -34,10 +23,10 @@
 	 bool     dll,
 	 ElfObjectFile *eof);
     const ElfObjectFile::secdata_t &getData() const;
+    uint32_t getSectionRvas(std::vector<section_mapping_t> &rvas) const;
+    void createHeaderSection(const std::vector<section_mapping_t> &rvas, u=
int32_t imageSize);
 =

 private:
-    void createHeaderSection();
-    uint32_t getSectionRvas(std::vector<section_mapping_t> &rvas) const;
     uint32_t getEntryPoint(const std::vector<section_mapping_t> &rvas, con=
st ElfObjectFile::Symbol *entry) const;
     int computeSize() const;
     int getExeFlags() const { return 0; }
@@ -47,7 +36,7 @@
     u32pair_t getResourceInfo(const std::vector<section_mapping_t> &rvas) =
const;
     u32pair_t getExceptionInfo() const;
     u32pair_t getSecurityInfo() const;
-    u32pair_t getRelocInfo() const;
+    u32pair_t getRelocInfo(const std::vector<section_mapping_t> &rvas) con=
st;
     u32pair_t getDebugInfo() const;
     u32pair_t getDescrInfo() const;
     u32pair_t getMachInfo() const;

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/imports.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/i=
mports.cpp?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/imports.cpp (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/imports.cpp Fri Mar  7 07:53:55 2008
@@ -1,0 +1,43 @@
+#include "imports.h"
+#include "objectfile.h"
+#include "section.h"
+
+void ImportFixup
+(ElfObjectFile &eof,
+ const std::vector<section_mapping_t> &mapping)
+{
+    const ElfObjectFile::Section *importSect =3D eof.getNamedSection(".ida=
ta");
+    if (!importSect) return;
+    uint8_t *importTable =3D importSect->getSectionData();
+    uint8_t *importTarget =3D importTable, *tableAddr, *hintName;
+    uint32_t importRva =3D FindRVA(mapping, importSect->getNumber());
+    uint32_t tableRva, iatRva, hintNameEntry;
+
+    do
+    {
+        le32write_postinc(importTarget, (tableRva =3D be32read(importTarge=
t)));
+        le32write_postinc(importTarget, be32read(importTarget));
+        le32write_postinc(importTarget, be32read(importTarget));
+        le32write_postinc(importTarget, be32read(importTarget));
+        le32write_postinc(importTarget, (iatRva =3D be32read(importTarget)=
));
+
+        if (!tableRva) return;
+
+        // Rewrite the import lookup table
+        tableAddr =3D importTable + tableRva - importRva;
+        while (hintNameEntry =3D be32read(tableAddr))
+        {
+            le32write_postinc(tableAddr, hintNameEntry);
+            // Rewrite the hint/name element
+            //hintName =3D importTable + hintNameEntry - importRva;
+            //le16write(hintName, be16read(hintName));
+        }
+
+        // Do the second address table
+        tableAddr =3D importTable + iatRva - importRva;
+        while (hintNameEntry =3D be32read(tableAddr))
+        {
+            le32write_postinc(tableAddr, hintNameEntry);
+        }
+    } while(1);
+}

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/imports.cpp
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/imports.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/i=
mports.h?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/imports.h (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/imports.h Fri Mar  7 07:53:55 2008
@@ -1,0 +1,14 @@
+#ifndef COMPDVR_IMPORTS_H
+#define COMPDVR_IMPORTS_H
+
+#include <vector>
+#include <utility>
+#include "pedef.h"
+#include "util.h"
+#include "objectfile.h"
+#include "section.h"
+
+void ImportFixup
+(ElfObjectFile &eof, const std::vector<section_mapping_t> &mapping);
+
+#endif//COMPDVR_IMPORTS_H

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/imports.h
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/o=
bjectfile.cpp?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.cpp (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.cpp Fri Mar  7 07:53:55 20=
08
@@ -3,43 +3,71 @@
 #include "util.h"
 #include "objectfile.h"
 =

-ElfObjectFile::ElfObjectFile(const std::string &filename) : fd(-1)
+ElfObjectFile::ElfObjectFile(const std::string &filename) : fd(-1), filena=
me(filename)
 {
-    Elf_Scn *s =3D 0;
+    init();
+}
+
+void ElfObjectFile::init()
+{
     Elf32_Ehdr *ehdr;
-    Section *sect;
+
     elfHeader =3D NULL;
+    lastStr =3D NULL;
+
     fd =3D open(filename.c_str(), O_RDWR, 0);
     if(fd >=3D 0) {
-	if(elf_version(EV_CURRENT) =3D=3D EV_NONE) {
-	    // Old version
-	    return;
-	}
-	elfHeader =3D elf_begin(fd, ELF_C_RDWR, (Elf*)0);
-	if(elf_kind(elfHeader) !=3D ELF_K_ELF) {
-	    // Didn't get an elf object file
+        if(elf_version(EV_CURRENT) =3D=3D EV_NONE) {
+            // Old version
+            return;
+        }
+        elfHeader =3D elf_begin(fd, ELF_C_RDWR, (Elf*)0);
+        if(elf_kind(elfHeader) !=3D ELF_K_ELF) {
+            // Didn't get an elf object file
             elfHeader =3D NULL;
-	    return;
-	}
-	ehdr =3D elf32_getehdr(elfHeader);
-	shnum =3D ehdr->e_shnum;
-	phnum =3D ehdr->e_phnum;
-	shstrndx =3D ehdr->e_shstrndx;
-	/* Populate section table */
-	for(size_t i =3D 0; i < shnum; i++)
-	{
-	    s =3D elf_nextscn(elfHeader, s);
-	    if(!s) break;
-	    sect =3D new Section(*this, i, s);
-	    sections.push_back(sect);
-	    sections_by_name.insert(std::make_pair(sect->getName(), sect));
-	}
+            return;
+        }
 =

-	populateSymbolTable();
+        ehdr =3D elf32_getehdr(elfHeader);
+        phnum =3D ehdr->e_phnum;
+        shstrndx =3D ehdr->e_shstrndx;
+
+        populateSections();
+        populateSymbolTable();
+    }
+}
+
+void ElfObjectFile::populateSections()
+{
+    Section *sect;
+    Elf32_Ehdr *ehdr;
+    Elf_Scn *s =3D 0;
+
+    ehdr =3D elf32_getehdr(elfHeader);
+    shnum =3D ehdr->e_shnum;
+    =

+    /* ABS section */
+    sections.clear();
+    sections_by_name.clear();
+    sections.push_back(new Section(*this, 0, NULL));
+    =

+    /* Populate section table */
+    for(size_t i =3D 1; i < shnum; i++)
+    {
+        s =3D elf_nextscn(elfHeader, s);
+        if(!s) break;
+        sect =3D new Section(*this, i, s);
+        sections.push_back(sect);
+        sections_by_name.insert(std::make_pair(sect->getName(), sect));
     }
 }
 =

 ElfObjectFile::~ElfObjectFile()
+{
+    finalize();
+}
+
+void ElfObjectFile::finalize()
 {
     if(elfHeader) elf_end(elfHeader);
     if(fd >=3D 0) close(fd);
@@ -55,7 +83,10 @@
     Elf32_Sym *sym;
     Symbol *ourSym;
 =

-    for( i =3D 0; i < getNumSections(); i++ ) {
+    symbols.clear();
+    symbols_by_name.clear();
+
+    for( i =3D 1; i < getNumSections(); i++ ) {
 	type =3D getSection(i).getType();
 	link =3D getSection(i).getLink();
 	if( (type =3D=3D SHT_SYMTAB) || (type =3D=3D SHT_DYNSYM) ) {
@@ -85,7 +116,7 @@
     /* Create data for the new section */
     Elf_Data *edata =3D elf_newdata(newsect), *strdata =3D elf_getdata(str=
sect, 0),
 	*newstrdata =3D elf_newdata(strsect);
-    edata->d_align =3D 0x1000;
+    edata->d_align =3D 1;
     edata->d_size =3D data.size();
     edata->d_off =3D 0;
     edata->d_type =3D ELF_T_BYTE;
@@ -93,10 +124,12 @@
     edata->d_buf =3D malloc(edata->d_size);
     memcpy(edata->d_buf, &data[0], edata->d_size);
     /* Add the name of the new section to the string table */
-    newstrdata->d_off =3D strdata->d_off + strdata->d_size;
+    if (!lastStr) lastStr =3D strdata;
+    newstrdata->d_off =3D lastStr->d_off + lastStr->d_size;
     newstrdata->d_size =3D name.size() + 1;
     newstrdata->d_align =3D 1;
     newstrdata->d_buf =3D (void *)name.c_str();
+    lastStr =3D newstrdata;
     /* Finish the section */
     shdr->sh_name =3D newstrdata->d_off;
     shdr->sh_type =3D type;

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/o=
bjectfile.h?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.h (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/objectfile.h Fri Mar  7 07:53:55 2008
@@ -78,11 +78,15 @@
 =

 	uint8_t *getSectionData() const {
 	    if(!have_data) {
-		data =3D *elf_getdata(section, NULL);
+                data =3D *elf_getdata(section, NULL);
 		have_data =3D true;
 	    }
 	    return (uint8_t *)data.d_buf;
 	}
+
+        void setDirty() const {
+            elf_flagscn(section, ELF_C_SET, ELF_F_DIRTY);
+        }
 =

     private:
 	const ElfObjectFile *obj;
@@ -109,18 +113,30 @@
     const Section *getNamedSection(const std::string &name) const;
     const Symbol &getSymbol(int n) const { return *symbols[n]; }
     const Symbol *getNamedSymbol(const std::string &symname) const;
+    void update() =

+    { =

+        elf_flagelf(elfHeader, ELF_C_SET, ELF_F_DIRTY);
+        elf_update(elfHeader, ELF_C_WRITE); =

+        finalize();
+        init();
+    }
 =

 private:
     int fd;
     int shnum, phnum;
     int shstrndx;
     Elf *elfHeader;
+    Elf_Data *lastStr;
+    std::string filename;
     std::vector<Section*> sections;
     std::map<std::string, const Section *> sections_by_name;
     std::vector<Symbol*> symbols;
     std::map<std::string, const Symbol *> symbols_by_name;
 =

+    void init();
+    void finalize();
     void populateSymbolTable();
+    void populateSections();
 };
 =

 #endif//COMPDVR_ELFOBJECT_H

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/r=
eloc.cpp?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp Fri Mar  7 07:53:55 2008
@@ -1,0 +1,192 @@
+#include "objectfile.h"
+#include "reloc.h"
+#include "section.h"
+#include "util.h"
+#include <assert.h>
+
+void AddReloc
+(std::vector<uint8_t> &reloc, =

+ uint32_t imageBase,
+ uint32_t &relocAddrOff,
+ uint32_t newReloc, int type)
+{
+    size_t oldsize =3D reloc.size(), oddsize =3D oldsize & sizeof(uint16_t=
);
+    uint8_t *relptr;
+    if (relocAddrOff =3D=3D (uint32_t)-1)
+    {
+	reloc.resize(sizeof(uint32_t) * 2 + sizeof(uint16_t));
+	relocAddrOff =3D 0;
+	*GetRelocTarget(reloc, relocAddrOff) =3D newReloc & ~0xfff;
+	relptr =3D &reloc[2 * sizeof(uint32_t)];
+    }
+    else if (type !=3D -1 && ((newReloc & ~0xfff) !=3D *GetRelocTarget(rel=
oc, relocAddrOff)))
+    {
+        reloc.resize(reloc.size() + 2 * sizeof(uint32_t) + sizeof(uint16_t=
) + oddsize);
+        relptr =3D &reloc[oldsize + oddsize];
+	uint8_t *oldptr =3D (uint8_t*)GetRelocTarget(reloc, relocAddrOff);
+	le32write_postinc(oldptr, *GetRelocTarget(reloc, relocAddrOff) - imageBas=
e);
+	le32write_postinc(oldptr, relptr - &reloc[0] - relocAddrOff);
+	relocAddrOff =3D relptr - &reloc[0];
+        *GetRelocTarget(reloc, relocAddrOff) =3D newReloc & ~0xfff;
+        relptr +=3D sizeof(uint32_t);
+	le32write_postinc(relptr, 0);
+    }
+    else
+    {
+        reloc.resize(reloc.size() + sizeof(uint16_t));
+        relptr =3D &reloc[oldsize];
+    }
+
+    le16write(relptr, type << 12 | newReloc & 0xfff);
+}
+
+#define ADDR24_MASK 0xfc000003
+
+void SingleReloc
+(const ElfObjectFile &eof,
+ uint8_t *relptr, int relocsize, =

+ const std::vector<section_mapping_t> &rvas,
+ const ElfObjectFile::Section &symbols,
+ const ElfObjectFile::Section &target,
+ std::vector<uint8_t> &relocSect,
+ uint32_t imageBase,
+ uint32_t &relocAddr)
+{
+    int j;
+    Elf32_Rela reloc =3D { 0 };
+    Elf32_Sym symbol;
+    uint8_t *symptr;
+    uint32_t S,A,P;
+    =

+    /* Get the reloc */
+    memcpy(&reloc, relptr, relocsize);
+#if 0
+    printf("RELOC: offset %08x info %08x addend %08x [%02x %06x]\n", =

+           reloc.r_offset, reloc.r_info, reloc.r_addend,
+           ELF32_R_TYPE(reloc.r_info), ELF32_R_SYM(reloc.r_info));
+#endif
+    =

+    /* Get the symbol */
+    symptr =3D &symbols.getSectionData()
+        [ELF32_R_SYM(reloc.r_info) * sizeof(symbol)];
+    memcpy(&symbol, symptr, sizeof(symbol));
+
+    /* Compute addends */
+    S =3D symbol.st_value - =

+        eof.getSection(symbol.st_shndx).getStartRva() + =

+        FindRVA(rvas, symbol.st_shndx) + =

+        imageBase;
+    A =3D reloc.r_addend;
+    P =3D reloc.r_offset + FindRVA(rvas, target.getNumber()) - target.getS=
tartRva();
+    //printf("start of target elf section %08x\n", target.getStartRva());
+
+#if 0
+    printf("SYMBOL: value %08x size %08x info %02x other %02x shndx %08x t=
otal %08x\n",
+           symbol.st_value,
+           symbol.st_size,
+           symbol.st_info,
+           symbol.st_other,
+           symbol.st_shndx,
+           S);
+#endif
+
+    uint8_t *Target =3D TargetPtr(rvas, target, P);
+    uint8_t *tword =3D TargetPtr(rvas, target, P & ~3);
+    uint8_t oldBytes[sizeof(uint32_t)];
+    memcpy(oldBytes, tword, sizeof(oldBytes));
+    =

+    P +=3D imageBase;
+
+    switch (ELF32_R_TYPE(reloc.r_info))
+    {
+    case R_PPC_NONE:
+        break;
+    case R_PPC_ADDR32:
+        //printf("ADDR32 S %08x A %08x P %08x\n", S, A, P);
+        be32write(Target, S + A);
+        AddReloc(relocSect, imageBase, relocAddr, P, 3);
+        break;
+    case R_PPC_REL32:
+        //printf("REL32 S %08x A %08x P %08x\n", S, A, P);
+        be32write(Target, S + A - P);
+        break;
+    case R_PPC_UADDR32: /* Special: Treat as RVA */
+        //printf("UADDR32 S %08x A %08x P %08x\n", S, A, P);
+        be32write(Target, S + A - imageBase);
+        break;
+    case R_PPC_REL24:
+        //printf("REL24 S %08x A %08x P %08x\n", S, A, P);
+        //printf("New Offset: %08x to Addr %08x from %08x\n", S+A-P, S+A, =
P);
+        be32write(Target, ((S+A-P) & ~ADDR24_MASK) | (be32read(Target) & A=
DDR24_MASK));
+        break;
+    case R_PPC_ADDR16_LO:
+        //printf("ADDR16_LO S %08x A %08x P %08x\n", S, A, P);
+        be16write(Target, S + A);
+        AddReloc(relocSect, imageBase, relocAddr, P, 2);
+        break;
+    case R_PPC_ADDR16_HA:
+        //printf("ADDR16_HA S %08x A %08x P %08x\n", S, A, P);
+        be16write(Target, (S + A + 0x8000) >> 16);
+        AddReloc(relocSect, imageBase, relocAddr, P, 4);
+	AddReloc(relocSect, imageBase, relocAddr, S + A, -1);
+        break;
+    default:
+        break;
+    }
+
+    uint8_t newBytes[sizeof(uint32_t)];
+    memcpy(newBytes, tword, sizeof(newBytes));
+#if 0
+    printf("Reloc changed %08x [%02x %02x %02x %02x] --> [%02x %02x %02x %=
02x]\n",
+           P & ~3,
+           oldBytes[0],
+           oldBytes[1],
+           oldBytes[2],
+           oldBytes[3],
+           newBytes[0],
+           newBytes[1],
+           newBytes[2],
+           newBytes[3]);
+#endif
+}
+
+void SingleRelocSection
+(const ElfObjectFile &obf, =

+ const ElfObjectFile::Section &section,
+ const std::vector<section_mapping_t> &rvas,
+ std::vector<uint8_t> &relocData,
+ uint32_t imageBase,
+ uint32_t &relocAddr)
+{
+    Elf32_Rela reloc =3D { };
+    uint8_t *Target;
+    int numreloc, relstart, relsize, j;
+    uint8_t *sectionData =3D section.getSectionData();
+    =

+    relsize =3D section.getType() =3D=3D SHT_RELA ? 12 : 8;
+    numreloc =3D section.logicalSize() / relsize;
+    const ElfObjectFile::Section &targetSection =3D obf.getSection(section=
.getInfo());
+
+    /* Don't relocate non-program section */
+    if (!(targetSection.getFlags() & SHF_ALLOC))
+	return;
+
+    targetSection.setDirty();
+
+    /* Get the symbol section */
+    const ElfObjectFile::Section &symbolSection =3D obf.getSection(section=
.getLink());
+
+    for(j =3D 0; j < numreloc; j++)
+    {
+        SingleReloc
+            (obf,
+             sectionData + j * relsize, =

+             relsize,
+	     rvas,
+             symbolSection,
+             targetSection,
+             relocData,
+             imageBase,
+	     relocAddr);
+    }
+}

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.cpp
---------------------------------------------------------------------------=
---
    svn:executable =3D *

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/r=
eloc.h?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h Fri Mar  7 07:53:55 2008
@@ -1,0 +1,22 @@
+#ifndef _ELFPE_RELOC_H
+#define _ELFPE_RELOC_H
+
+#include <vector>
+#include "section.h"
+#include "objectfile.h"
+
+void SingleRelocSection
+(const ElfObjectFile &obf,
+ const ElfObjectFile::Section &section,
+ const std::vector<section_mapping_t> &rvas,
+ std::vector<uint8_t> &relocData,
+ uint32_t imageBase,
+ uint32_t &relocAddr);
+
+void AddReloc
+(std::vector<uint8_t> &reloc, =

+ uint32_t imageBase,
+ uint32_t &relocAddrOff,
+ uint32_t newReloc, int type);
+
+#endif//_ELFPE_RELOC_H

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/reloc.h
---------------------------------------------------------------------------=
---
    svn:executable =3D *

Added: trunk/tools/RosBE/RosBE-PPC/elfpe/section.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/s=
ection.h?rev=3D32591&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/section.h (added)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/section.h Fri Mar  7 07:53:55 2008
@@ -1,0 +1,16 @@
+#ifndef COMPDVR_SECTION_H
+#define COMPDVR_SECTION_H
+
+typedef struct section_mapping_t {
+    const ElfObjectFile::Section *section;
+    uint32_t rva;
+    int index;
+    =

+    section_mapping_t
+    (const ElfObjectFile::Section *sect, uint32_t rva, int index) :
+	section(sect), rva(rva), index(index) { }
+    section_mapping_t(const section_mapping_t &other) :
+	section(other.section), rva(other.rva), index(other.index) { }
+} section_mapping_t;
+
+#endif//COMPDVR_SECTION_H

Propchange: trunk/tools/RosBE/RosBE-PPC/elfpe/section.h
---------------------------------------------------------------------------=
---
    svn:eol-style =3D native

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/util.cpp
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/u=
til.cpp?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/util.cpp (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/util.cpp Fri Mar  7 07:53:55 2008
@@ -1,4 +1,5 @@
 #include "util.h"
+#include <assert.h>
 =

 uint32_t roundup(uint32_t value, int round)
 {
@@ -34,7 +35,29 @@
     le32write_postinc(dataptr, value.second);
 }
 =

-uint16_t be16read(uint8_t *dataptr)
+void be16write_postinc(uint8_t *&dataptr, uint16_t value)
+{
+    *dataptr++ =3D value >> 8;
+    *dataptr++ =3D value;
+}
+
+void be16write(uint8_t *dataptr, uint16_t value)
+{
+    be16write_postinc(dataptr, value);
+}
+
+void be32write_postinc(uint8_t *&dataptr, uint32_t value)
+{
+    be16write_postinc(dataptr, value >> 16);
+    be16write_postinc(dataptr, value);
+}
+
+void be32write(uint8_t *dataptr, uint32_t value)
+{
+    be32write_postinc(dataptr, value);
+}
+
+uint16_t be16read(uint8_t *dataptr) =

 {
     return dataptr[0] << 8 | dataptr[1];
 }
@@ -48,7 +71,7 @@
 =

 uint32_t be32read(uint8_t *dataptr)
 {
-    return be16read(dataptr) << 16 | be16read(dataptr+2);
+    return (be16read(dataptr) << 16) | be16read(dataptr+2);
 }
 =

 uint32_t be32read_postinc(uint8_t *&dataptr)
@@ -57,3 +80,35 @@
     dataptr +=3D 4;
     return res;
 }
+
+uint32_t FindRVA(const std::vector<section_mapping_t> &mapping, int secnum)
+{
+    int i;
+    for (i =3D 0; i < mapping.size(); i++)
+	if (mapping[i].section->getNumber() =3D=3D secnum) return mapping[i].rva;
+    return 0;
+}
+
+uint8_t *TargetPtr
+(const std::vector<section_mapping_t> &mapping, =

+ const ElfObjectFile::Section &target, uint32_t va)
+{
+    uint32_t srva =3D FindRVA(mapping, target.getNumber());
+    uint32_t off =3D va - srva;
+#if 0
+    printf
+        ("Relocating against VA %08x in section (%08x-%08x) %s\n", =

+         va, =

+         srva, =

+         srva + target.logicalSize(), =

+         target.getName().c_str());
+#endif
+    assert(off < target.logicalSize());
+    return target.getSectionData() + off;
+}
+
+uint32_t *GetRelocTarget(const std::vector<uint8_t> &reloc, uint32_t off)
+{
+    return (uint32_t *)(&reloc[0] + off);
+}
+

Modified: trunk/tools/RosBE/RosBE-PPC/elfpe/util.h
URL: http://svn.reactos.org/svn/reactos/trunk/tools/RosBE/RosBE-PPC/elfpe/u=
til.h?rev=3D32591&r1=3D32590&r2=3D32591&view=3Ddiff
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/tools/RosBE/RosBE-PPC/elfpe/util.h (original)
+++ trunk/tools/RosBE/RosBE-PPC/elfpe/util.h Fri Mar  7 07:53:55 2008
@@ -1,8 +1,11 @@
 #ifndef COMPDVR_UTIL_H
 #define COMPDVR_UTIL_H
 =

+#include <vector>
 #include <utility>
 #include <stdint.h>
+#include "objectfile.h"
+#include "section.h"
 =

 void le16write(uint8_t *dataptr, uint16_t value);
 void le16write_postinc(uint8_t *&dataptr, uint16_t value);
@@ -12,6 +15,10 @@
 uint16_t le16read_postinc(uint8_t *&dataptr);
 uint32_t le32read(uint8_t *dataptr);
 uint32_t le32read_postinc(uint8_t *&dataptr);
+void be16write(uint8_t *dataptr, uint16_t value);
+void be16write_postinc(uint8_t *&dataptr, uint16_t value);
+void be32write(uint8_t *dataptr, uint32_t value);
+void be32write_postinc(uint8_t *&dataptr, uint32_t value);
 uint16_t be16read(uint8_t *dataptr);
 uint16_t be16read_postinc(uint8_t *&dataptr);
 uint32_t be32read(uint8_t *dataptr);
@@ -21,4 +28,10 @@
 void le32pwrite(uint8_t *dataptr, const u32pair_t &pair);
 uint32_t roundup(uint32_t value, int round);
 =

+uint32_t FindRVA(const std::vector<section_mapping_t> &mapping, int secnum=
);
+uint8_t *TargetPtr
+(const std::vector<section_mapping_t> &mapping, =

+ const ElfObjectFile::Section &target, uint32_t va);
+uint32_t *GetRelocTarget(const std::vector<uint8_t> &reloc, uint32_t off);
+
 #endif//COMPDVR_UTIL_H




More information about the Ros-diffs mailing list