Author: arty
Date: Tue May 1 11:20:45 2007
New Revision: 26604
URL:
http://svn.reactos.org/svn/reactos?rev=26604&view=rev
Log:
Final commits for elf conversion.
Modified:
branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/mboot.c
branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/prep_vga.c
branches/powerpc/reactos/tools/ppc-build/elfpe/compdvr.cpp
branches/powerpc/reactos/tools/ppc-build/elfpe/header.cpp
branches/powerpc/reactos/tools/ppc-build/elfpe/header.h
branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.cpp
branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.h
branches/powerpc/reactos/tools/ppc-build/elfpe/pedef.h
branches/powerpc/reactos/tools/ppc-build/elfpe/util.cpp
branches/powerpc/reactos/tools/ppc-build/elfpe/util.h
Modified: branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/mboot.c
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/boot/freeldr/fr…
==============================================================================
--- branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/mboot.c (original)
+++ branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/mboot.c Tue May 1 11:20:45
2007
@@ -398,6 +398,9 @@
* MemLoadAddr -- Freeldr address of module
* KernelAddr -- Kernel address of module
*--*/
+#define ELF_SECTION(n) ((Elf32_Shdr*)(sptr + (n * shsize)))
+#define COFF_FIRST_SECTION(h) ((PIMAGE_SECTION_HEADER)
((DWORD)h+FIELD_OFFSET(IMAGE_NT_HEADERS,OptionalHeader)+(SWAPW(((PIMAGE_NT_HEADERS)(h))->FileHeader.SizeOfOptionalHeader))))
+
BOOLEAN
NTAPI
FrLdrMapModule(FILE *KernelImage, PCHAR ImageName, PCHAR MemLoadAddr, ULONG KernelAddr)
@@ -410,7 +413,6 @@
INT i, j;
PLOADER_MODULE ModuleData;
int phsize, phnum, shsize, shnum, relsize, SectionAddr = 0;
- PULONG SectionOffsets;
PCHAR sptr;
Elf32_Ehdr ehdr;
Elf32_Shdr *shdr;
@@ -441,7 +443,6 @@
shsize = ehdr.e_shentsize;
shnum = ehdr.e_shnum;
sptr = (PCHAR)MmAllocateMemory(shnum * shsize);
- SectionOffsets = (PULONG)MmAllocateMemory(shnum * sizeof(ULONG));
/* Read section headers */
FsSetFilePointer(KernelImage, ehdr.e_shoff);
@@ -452,8 +453,8 @@
/* Now we'll get the PE Header */
for( i = 0; i < shnum; i++ )
{
- shdr = (Elf32_Shdr*)(sptr + (i * shsize));
- printf("Header %d: type %d flags %x\n", i, shdr->sh_type,
shdr->sh_flags);
+ shdr = ELF_SECTION(i);
+
/* Find the PE Header */
if (shdr->sh_type == TYPE_PEHEADER)
{
@@ -462,7 +463,6 @@
ImageHeader = (PIMAGE_DOS_HEADER)KernelMemory;
NtHeader = (PIMAGE_NT_HEADERS)((PCHAR)KernelMemory +
SWAPD(ImageHeader->e_lfanew));
printf("NtHeader at %x\n", SWAPD(ImageHeader->e_lfanew));
- SectionOffsets[i] = 0;
printf("SectionAlignment %x\n",
SWAPD(NtHeader->OptionalHeader.SectionAlignment));
SectionAddr = ROUND_UP
@@ -478,38 +478,46 @@
return 0;
}
- /* Save the Image Size */
+ /* Save the Image Base */
NtHeader->OptionalHeader.ImageBase = SWAPD(KernelAddr);
/* Load the file image */
- Section = IMAGE_FIRST_SECTION(NtHeader);
- SectionCount = NtHeader->FileHeader.NumberOfSections - 1;
+ Section = COFF_FIRST_SECTION(NtHeader);
+ SectionCount = SWAPW(NtHeader->FileHeader.NumberOfSections);
+
+ printf("Section headers at %x\n", ((PCHAR)Section) - ((PCHAR)KernelAddr));
+
+ printf("-- Dumping ntoskrnl memory --\n");
+ for (i = 0; i < 0x1000; i++) {
+ if(!(i & 0xf)) {
+ if(i) printf("\n");
+ for (j = 0; j < 8; j++)
+ printf("%x", (((KernelAddr + i) << (j * 4)) >> 28) & 0xf);
+ printf(": ");
+ }
+ printf("%x%x ",
+ (*(((PCHAR)KernelMemory)+i)>>4)&0xf,
+ *(((PCHAR)KernelMemory)+i)&0xf);
+ }
+ printf("-- Dump done --\n");
/* Walk each section */
for (i=0; i < SectionCount; i++, Section++)
{
- shdr = (Elf32_Shdr*)(sptr + (i * shsize));
- if ((shdr->sh_type != SHT_PROGBITS) &&
- (shdr->sh_type != SHT_NOBITS)) continue;
-
- SectionOffsets[i] = SectionAddr;
+ printf("Section %d (NT Header) is elf section %d\n",
+ i, SWAPD(Section->PointerToRawData));
+ shdr = ELF_SECTION(SWAPD(Section->PointerToRawData));
+
+ shdr->sh_addr = SectionAddr = SWAPD(Section->VirtualAddress);
- shdr = (Elf32_Shdr*)(sptr + (i * shsize));
-
- /* Get the disk location and the memory location, and the size */
- Section->Misc.VirtualSize = SWAPD(shdr->sh_size);
- Section->VirtualAddress = SWAPD(SectionAddr);
- Section->SizeOfRawData = SWAPD(shdr->sh_size);
- Section->PointerToRawData = SWAPD(shdr->sh_offset);
-
- if (shdr->sh_type == SHT_PROGBITS)
+ if (shdr->sh_type != SHT_NOBITS)
{
/* Content area */
printf("Loading section %d at %x\n", i, KernelAddr + SectionAddr);
FsSetFilePointer(KernelImage, shdr->sh_offset);
FsReadFile(KernelImage, shdr->sh_size, NULL, KernelMemory + SectionAddr);
}
- else if (shdr->sh_type == SHT_NOBITS)
+ else
{
/* Zero it out */
printf("BSS section %d at %x\n", i, KernelAddr + SectionAddr);
@@ -517,18 +525,14 @@
ROUND_UP(shdr->sh_size,
SWAPD(NtHeader->OptionalHeader.SectionAlignment)));
}
-
- SectionAddr +=
- ROUND_UP
- (shdr->sh_size, SWAPD(NtHeader->OptionalHeader.SectionAlignment));
- }
-
- ImageSize = SectionAddr;
- NtHeader->OptionalHeader.SizeOfImage = SWAPD(ImageSize);
+ }
+
+ ImageSize = SWAPD(NtHeader->OptionalHeader.SizeOfImage);
+ KernelEntry = SWAPD(NtHeader->OptionalHeader.AddressOfEntryPoint);
printf("Total image size is %x\n", ImageSize);
/* Handle relocation sections */
- for (Section = IMAGE_FIRST_SECTION(NtHeader), i = 0;
+ for (Section = COFF_FIRST_SECTION(NtHeader), i = 0;
i < SectionCount;
i++, Section++) {
Elf32_Rela reloc = { };
@@ -537,7 +541,7 @@
int numreloc, relstart, targetSection;
Elf32_Sym symbol;
- shdr = (Elf32_Shdr*)(sptr + (i * shsize));
+ shdr = ELF_SECTION(i);
/* Only relocs here */
if((shdr->sh_type != SHT_REL) &&
(shdr->sh_type != SHT_RELA)) continue;
@@ -551,12 +555,13 @@
i, shdr->sh_link, shdr->sh_info);
/* Get the symbol section */
- shdr = (Elf32_Shdr*)(sptr + (shdr->sh_link * shsize));
+ shdr = ELF_SECTION(shdr->sh_link);
for(j = 0; j < numreloc; j++)
{
ULONG S,A,P;
+
/* Get the reloc */
FsSetFilePointer(KernelImage, relstart + (numreloc * relsize));
FsReadFile(KernelImage, sizeof(reloc), NULL, &reloc);
@@ -566,12 +571,12 @@
FsReadFile(KernelImage, sizeof(symbol), NULL, &symbol);
/* Compute addends */
- S = symbol.st_value + KernelAddr + SectionOffsets[symbol.st_shndx];
+ S = symbol.st_value + KernelAddr + ELF_SECTION(symbol.st_shndx)->sh_addr;
A = reloc.r_addend;
- P = reloc.r_offset + KernelAddr + SectionOffsets[targetSection];
+ P = reloc.r_offset + KernelAddr + ELF_SECTION(targetSection)->sh_addr;
Target32 =
- (ULONG*)(((PCHAR)KernelMemory) + SectionOffsets[targetSection]);
+ (ULONG*)(((PCHAR)KernelMemory) + ELF_SECTION(targetSection)->sh_addr);
Target16 = (USHORT *)Target32;
switch (ELF32_R_TYPE(reloc.r_info))
@@ -603,22 +608,6 @@
}
}
-#if 0
- printf("-- Dumping ntoskrnl memory --\n");
- for (i = 0; i < ImageSize; i++) {
- if(!(i & 0xf)) {
- if(i) printf("\n");
- for (j = 0; j < 8; j++)
- printf("%x", (((KernelAddr + i) << (j * 4)) >> 28) & 0xf);
- printf(": ");
- }
- printf("%x%x ",
- (*(((PCHAR)KernelMemory)+i)>>4)&0xf,
- *(((PCHAR)KernelMemory)+i)&0xf);
- }
- printf("-- Dump done --\n");
-#endif
-
ModuleData = &reactos_modules[LoaderBlock.ModsCount];
ModuleData->ModStart = (ULONG)KernelMemory;
/* Increase the next Load Base */
Modified: branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/prep_vga.c
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/boot/freeldr/fr…
==============================================================================
--- branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/prep_vga.c (original)
+++ branches/powerpc/reactos/boot/freeldr/freeldr/arch/powerpc/prep_vga.c Tue May 1
11:20:45 2007
@@ -1,5 +1,8 @@
#include <freeldr.h>
#include "prep.h"
+#include "ppcboot.h"
+
+extern boot_infos_t BootInfo;
struct _vga_desc {
char *port;
@@ -20,6 +23,7 @@
print_bar( &bar_data );
if( (bar_data.data > 0x10000) || ((bar_data.data&1) == 1) ) {
vga_desc->addr = (char *)(0xc0000000 + (bar_data.data & ~0x7ff));
+ BootInfo.dispDeviceBase = vga_desc->addr;
break;
}
}
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/compdvr.cpp
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/compdvr.cpp (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/compdvr.cpp Tue May 1 11:20:45 2007
@@ -127,11 +127,12 @@
bool use_libgcc = false;
std::vector<std::string> args = arg_vect;
std::string real_output,
- entry_point = "0x1000",
+ entry_point = "_main",
image_base = "0x400000", subsystem = "windows",
make_dll,
file_align = "0x1000",
section_align = "0x1000", base_file;
+ const ElfObjectFile::Symbol *entry_sym;
std::vector<std::string>::iterator i =
std::find(args.begin(),args.end(),"-lgcc");
@@ -163,8 +164,6 @@
args.insert(args.begin()+1,"-T");
args.insert(args.begin()+2,ldscript);
args.insert(args.begin()+1,"-r");
- args.insert(args.begin()+1,"--start-group");
- args.push_back("--end-group");
for( size_t i = 0; i < args.size(); i++ ) {
if( args[i] == "-o" && i < args.size()-1 ) {
@@ -201,16 +200,19 @@
if(!eof) return 1;
+ entry_sym = eof.getNamedSymbol(entry_point);
+
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,
- atoi(subsystem.c_str()),
&eof);
eof.addSection(".peheader", header.getData(), TYPE_PEHEADER);
@@ -307,7 +309,9 @@
if( subcmd_args[0].find("collect2") != std::string::npos ||
subcmd_args[0].find("ld") != std::string::npos ) {
- if( run_ld( verbose, nostdlib, nostartfiles, is_dll, make_map,
+ subcmd_args[0] = linker_name;
+ if( status =
+ run_ld( verbose, nostdlib, nostartfiles, is_dll, make_map,
mingw_lib_dir, ldscript, subcmd_args ) )
goto final;
else
@@ -317,7 +321,7 @@
if( verbose )
subcmd_args.insert(subcmd_args.begin()+1,"-v");
- if( execute_command
+ if( status = execute_command
( verbose, subcmd_args ) )
goto final;
} else if( verbose )
@@ -326,7 +330,6 @@
goto theend;
final:
- status = 1;
theend:
if( verbose ) {
fprintf( stderr, "<status>%d</status>\n", status );
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/header.cpp
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/header.cpp (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/header.cpp Tue May 1 11:20:45 2007
@@ -6,6 +6,7 @@
(uint32_t imagebase,
uint32_t filealign,
uint32_t sectionalign,
+ const ElfObjectFile::Symbol *entry,
uint32_t stackreserve,
uint32_t stackcommit,
uint32_t heapreserve,
@@ -20,6 +21,7 @@
heapreserve(heapreserve),
heapcommit(heapcommit),
subsysid(subsysid),
+ entry(entry),
eof(eof)
{
data.resize(computeSize());
@@ -33,6 +35,9 @@
void ElfPeHeader::createHeaderSection()
{
+ std::vector<section_mapping_t> sectionRvaSet;
+ uint32_t imageSize = getSectionRvas(sectionRvaSet);
+
data[0] = 'M'; data[1] = 'Z';
uint8_t *dataptr = &data[0x3c];
uint32_t coffHeaderSize, optHeaderSizeMember;
@@ -40,7 +45,7 @@
dataptr = &data[0x80];
le32write_postinc(dataptr, 0x4550);
le16write_postinc(dataptr, getPeArch());
- le16write_postinc(dataptr, getNumSections());
+ le16write_postinc(dataptr, sectionRvaSet.size());
le32write_postinc(dataptr, time(NULL));
le32write_postinc(dataptr, 0);
le32write_postinc(dataptr, 0);
@@ -53,7 +58,7 @@
le32write_postinc(dataptr, 0);
le32write_postinc(dataptr, 0);
le32write_postinc(dataptr, 0);
- le32write_postinc(dataptr, getEntryPoint());
+ le32write_postinc(dataptr, getEntryPoint(sectionRvaSet, entry));
le32write_postinc(dataptr, 0);
le32write_postinc(dataptr, 0);
le32write_postinc(dataptr, imagebase);
@@ -66,7 +71,7 @@
le16write_postinc(dataptr, 4);
le16write_postinc(dataptr, 0);
le32write_postinc(dataptr, 0);
- le32write_postinc(dataptr, getImageSize());
+ le32write_postinc(dataptr, imageSize);
le32write_postinc(dataptr, computeSize());
le32write_postinc(dataptr, 0); // No checksum yet
le16write_postinc(dataptr, subsysid);
@@ -92,18 +97,50 @@
le16write
(&data[0] + optHeaderSizeMember,
(dataptr - &data[0]) - coffHeaderSize);
+ // Here, we store references to the sections, filling in the RVA and
+ // 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
+ for (int i = 0; i < sectionRvaSet.size(); i++)
+ {
+ section_mapping_t mapping = sectionRvaSet[i];
+ const ElfObjectFile::Section *section = mapping.section;
+ std::string name = section->getName();
+ uint32_t size = section->logicalSize();
+ uint32_t rva = mapping.rva;
+ for (int j = 0; j < 8; j++)
+ {
+ *dataptr++ = j < name.size() ? name[j] : '\000';
+ }
+ 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, 0);
+ le32write_postinc(dataptr, 0);
+ le32write_postinc(dataptr, 0);
+ // XXX Figure out the real flags
+ le32write_postinc(dataptr, IMAGE_SCN_CNT_CODE);
+ }
}
const std::vector<uint8_t> &ElfPeHeader::getData() const { return data; }
-uint32_t ElfPeHeader::getImageSize() const
-{
- uint32_t start = 0;
- uint32_t limit = 0;
+uint32_t ElfPeHeader::getSectionRvas(std::vector<section_mapping_t> &rvas)
const
+{
+ uint32_t start = computeSize();
+ uint32_t limit = start;
for(int i = 0; i < eof->getNumSections(); i++) {
{
const ElfObjectFile::Section § = eof->getSection(i);
- limit = roundup(start + sect.logicalSize(), sectionalign);
+ if(sect.getType() == SHT_PROGBITS) {
+ limit = roundup(start + sect.logicalSize(), sectionalign);
+ fprintf(stderr, "rva[%02d:%s] = (%x %x %d)\n",
+ rvas.size(), sect.getName().c_str(), §, start, i);
+ rvas.push_back(section_mapping_t(§, start, i));
+ }
}
start = limit;
}
@@ -174,3 +211,15 @@
{
return std::make_pair(0,0);
}
+
+uint32_t ElfPeHeader::getEntryPoint
+(const std::vector<ElfPeHeader::section_mapping_t> &secmap,
+ const ElfObjectFile::Symbol *entry) const
+{
+ if(entry == NULL) return computeSize();
+ for(int i = 0; i < secmap.size(); i++) {
+ if(secmap[i].index == entry->section)
+ return secmap[i].rva + entry->offset;
+ }
+ return computeSize();
+}
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/header.h
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/header.h (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/header.h Tue May 1 11:20:45 2007
@@ -13,6 +13,7 @@
(uint32_t imagebase,
uint32_t imagealign,
uint32_t filealign,
+ const ElfObjectFile::Symbol *entry,
uint32_t stackreserve,
uint32_t stackcommit,
uint32_t heapreserve,
@@ -23,8 +24,21 @@
const ElfObjectFile::secdata_t &getData() const;
private:
+ 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;
+
void createHeaderSection();
- int getNumSections() const { return eof->getNumSections(); }
+ uint32_t getSectionRvas(std::vector<section_mapping_t> &rvas) const;
+ uint32_t getEntryPoint(const std::vector<section_mapping_t> &rvas, const
ElfObjectFile::Symbol *entry) const;
int computeSize() const;
int getExeFlags() const { return 0; }
int getDllFlags() const { return dll ? IMAGE_FILE_DLL : 0; }
@@ -38,8 +52,6 @@
u32pair_t getDescrInfo() const;
u32pair_t getMachInfo() const;
u32pair_t getTlsInfo() const;
- uint32_t getEntryPoint() const { return eof->getEntryPoint(); }
- uint32_t getImageSize() const;
uint16_t getPeArch() const;
uint32_t saToRva(int section, uint32_t offset) const;
uint32_t vaToRva(uint32_t source_addr) const;
@@ -50,6 +62,7 @@
heapreserve, heapcommit;
bool dll;
int subsysid;
+ const ElfObjectFile::Symbol *entry;
ElfObjectFile *eof;
std::vector<uint8_t> data;
static const char *mzHeader;
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.cpp
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.cpp (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.cpp Tue May 1 11:20:45
2007
@@ -1,11 +1,13 @@
#include <libelf/libelf.h>
#include <fcntl.h>
+#include "util.h"
#include "objectfile.h"
ElfObjectFile::ElfObjectFile(const std::string &filename) : fd(-1)
{
Elf_Scn *s = 0;
Elf32_Ehdr *ehdr;
+ Section *sect;
fd = open(filename.c_str(), O_RDWR, 0);
if(fd >= 0) {
if(elf_version(EV_CURRENT) == EV_NONE) {
@@ -21,13 +23,17 @@
shnum = ehdr->e_shnum;
phnum = ehdr->e_phnum;
shstrndx = ehdr->e_shstrndx;
+ /* Populate section table */
for(size_t i = 0; i < shnum; i++)
{
s = elf_nextscn(elfHeader, s);
if(!s) break;
- sections.push_back(new Section(*this, s));
- fprintf(stderr, "Got section %04d %s\n", i,
sections[i]->getName().c_str());
+ sect = new Section(*this, s);
+ sections.push_back(sect);
+ sections_by_name.insert(std::make_pair(sect->getName(), sect));
}
+
+ populateSymbolTable();
}
}
@@ -35,6 +41,32 @@
{
if(elfHeader) elf_end(elfHeader);
if(fd >= 0) close(fd);
+}
+
+void ElfObjectFile::populateSymbolTable()
+{
+ int i = 0, j;
+ int type, link, flags, section;
+ uint32_t offset;
+ std::string name;
+ uint8_t *data, *symptr;
+ Elf32_Sym *sym;
+ Symbol *ourSym;
+
+ for( i = 0; i < getNumSections(); i++ ) {
+ type = getSection(i).getType();
+ link = getSection(i).getLink();
+ if( (type == SHT_SYMTAB) || (type == SHT_DYNSYM) ) {
+ /* Read a symbol */
+ sym = (Elf32_Sym*)getSection(i).getSectionData();
+ for (j = 0; j < getSection(i).logicalSize() / sizeof(Elf32_Sym); j++) {
+ name = elf_strptr(elfHeader, link, sym[j].st_name);
+ ourSym = new Symbol(name, sym[j].st_value, sym[j].st_shndx, sym[j].st_info);
+ symbols.push_back(ourSym);
+ symbols_by_name.insert(std::make_pair(name, ourSym));
+ }
+ }
+ }
}
uint32_t ElfObjectFile::getEntryPoint() const
@@ -66,7 +98,7 @@
/* Finish the section */
shdr->sh_name = newstrdata->d_off;
shdr->sh_type = type;
- shdr->sh_flags = 0;
+ shdr->sh_flags = SHF_ALLOC;
shdr->sh_addr = 0;
shdr->sh_link = 0;
shdr->sh_info = 0;
@@ -76,8 +108,18 @@
const ElfObjectFile::Section *ElfObjectFile::getNamedSection(const std::string &name)
const
{
- for(size_t i = 0; i < sections.size(); i++) {
- if(sections[i]->getName() == name) return sections[i];
- }
- return NULL;
+ std::map<std::string, const ElfObjectFile::Section *>::const_iterator i =
+ sections_by_name.find(name);
+ if(i != sections_by_name.end())
+ return i->second;
+ else return NULL;
}
+
+const ElfObjectFile::Symbol *ElfObjectFile::getNamedSymbol(const std::string &name)
const
+{
+ std::map<std::string, const ElfObjectFile::Symbol *>::const_iterator i =
+ symbols_by_name.find(name);
+ if(i != symbols_by_name.end())
+ return i->second;
+ else return NULL;
+}
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.h
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.h (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/objectfile.h Tue May 1 11:20:45 2007
@@ -3,41 +3,83 @@
#include <string>
#include <vector>
+#include <map>
#include <libelf/libelf.h>
class ElfObjectFile {
public:
typedef std::vector<uint8_t> secdata_t;
+ class Symbol {
+ public:
+ Symbol(const std::string &name, uint32_t offset,
+ int section, int flags) :
+ name(name), offset(offset), section(section), flags(flags) { }
+ std::string name;
+ uint32_t offset;
+ int section;
+ int flags;
+ };
+
class Section {
public:
Section(const Section &other) :
- obj(other.obj), section(other.section) { }
+ obj(other.obj), section(other.section), have_data(false) {
+ e32shdr = elf32_getshdr(section);
+ }
Section(ElfObjectFile &obj, Elf_Scn *sechdr) :
- obj(&obj), section(sechdr) { }
+ obj(&obj), section(sechdr), have_data(false) {
+ e32shdr = elf32_getshdr(section);
+ }
Section &operator = (const Section &other) {
obj = other.obj;
+ have_data = false;
section = other.section;
+ e32shdr = other.e32shdr;
}
operator bool () { return !!section; }
std::string getName() const {
- Elf32_Shdr *e32shdr = elf32_getshdr(section);
return obj->getString(e32shdr->sh_name);
}
+ int getType() const {
+ return e32shdr->sh_type;
+ }
+
+ int getLink() const {
+ return e32shdr->sh_link;
+ }
+
+ int getInfo() const {
+ return e32shdr->sh_info;
+ }
+
int logicalSize() const {
- Elf32_Shdr *e32shdr = elf32_getshdr(section);
return e32shdr->sh_size;
}
uint32_t getStartRva() const {
- Elf32_Shdr *e32shdr = elf32_getshdr(section);
return e32shdr->sh_addr;
+ }
+
+ uint32_t getFileOffset() const {
+ return e32shdr->sh_offset;
+ }
+
+ uint8_t *getSectionData() const {
+ if(!have_data) {
+ data = *elf_getdata(section, NULL);
+ have_data = true;
+ }
+ return (uint8_t *)data.d_buf;
}
private:
const ElfObjectFile *obj;
Elf_Scn *section;
+ Elf32_Shdr *e32shdr;
+ mutable bool have_data;
+ mutable Elf_Data data;
};
ElfObjectFile(const std::string &filename);
@@ -54,6 +96,8 @@
(const std::string &name, const secdata_t &data, int type = SHT_PROGBITS);
const Section &getSection(int sect) const { return *sections[sect]; }
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;
private:
int fd;
@@ -61,6 +105,11 @@
int shstrndx;
Elf *elfHeader;
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 populateSymbolTable();
};
#endif//COMPDVR_ELFOBJECT_H
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/pedef.h
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/pedef.h (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/pedef.h Tue May 1 11:20:45 2007
@@ -29,4 +29,16 @@
#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
+#define IMAGE_SCN_CNT_CODE 0x00000020
+#define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
+#define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
+
+#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000
+#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000
+#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000
+#define IMAGE_SCN_MEM_SHARED 0x10000000
+#define IMAGE_SCN_MEM_EXECUTE 0x20000000
+#define IMAGE_SCN_MEM_READ 0x40000000
+#define IMAGE_SCN_MEM_WRITE 0x80000000
+
#endif//COMPDVR_PEDEF_H
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/util.cpp
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/util.cpp (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/util.cpp Tue May 1 11:20:45 2007
@@ -33,3 +33,27 @@
le32write_postinc(dataptr, value.first);
le32write_postinc(dataptr, value.second);
}
+
+uint16_t be16read(uint8_t *dataptr)
+{
+ return dataptr[0] << 8 | dataptr[1];
+}
+
+uint16_t be16read_postinc(uint8_t *&dataptr)
+{
+ uint16_t res = be16read(dataptr);
+ dataptr += 2;
+ return res;
+}
+
+uint32_t be32read(uint8_t *dataptr)
+{
+ return be16read(dataptr) << 16 | be16read(dataptr+2);
+}
+
+uint32_t be32read_postinc(uint8_t *&dataptr)
+{
+ uint32_t res = be32read(dataptr);
+ dataptr += 4;
+ return res;
+}
Modified: branches/powerpc/reactos/tools/ppc-build/elfpe/util.h
URL:
http://svn.reactos.org/svn/reactos/branches/powerpc/reactos/tools/ppc-build…
==============================================================================
--- branches/powerpc/reactos/tools/ppc-build/elfpe/util.h (original)
+++ branches/powerpc/reactos/tools/ppc-build/elfpe/util.h Tue May 1 11:20:45 2007
@@ -12,6 +12,10 @@
uint16_t le16read_postinc(uint8_t *&dataptr);
uint32_t le32read(uint8_t *dataptr);
uint32_t le32read_postinc(uint8_t *&dataptr);
+uint16_t be16read(uint8_t *dataptr);
+uint16_t be16read_postinc(uint8_t *&dataptr);
+uint32_t be32read(uint8_t *dataptr);
+uint32_t be32read_postinc(uint8_t *&dataptr);
typedef std::pair<uint32_t, uint32_t> u32pair_t;
void le32pwrite_postinc(uint8_t *&dataptr, const u32pair_t &pair);
void le32pwrite(uint8_t *dataptr, const u32pair_t &pair);