From ba2f00dba11c9cb940d87e6162d7508512a34960 Mon Sep 17 00:00:00 2001 From: Wolfgang Thaller Date: Tue, 8 Sep 2015 04:00:14 +0200 Subject: [PATCH] MakePEF: take symbols, relocs from loader section, entrypoint from aout header --- PEFTools/MakePEF.cc | 202 +++++++++++++------------------------------- 1 file changed, 59 insertions(+), 143 deletions(-) diff --git a/PEFTools/MakePEF.cc b/PEFTools/MakePEF.cc index 1062dcaecc..f5d5ef0e49 100755 --- a/PEFTools/MakePEF.cc +++ b/PEFTools/MakePEF.cc @@ -119,7 +119,8 @@ class ImportLib public: std::string path, base, mem; std::vector imports; - + std::vector xcoffImportIndices; + int nameOffset; bool weak; @@ -131,12 +132,17 @@ public: } }; -void mkpef(std::istream& in, std::ostream& out, std::string mainSymbol = "__start" ) +void mkpef(const std::string& inFn, const std::string& outFn) { + std::ifstream in(inFn); + external_filehdr xcoffHeader; - + external_aouthdr aoutHeader; + in.read((char*) &xcoffHeader, sizeof(xcoffHeader)); - in.seekg(getI16(xcoffHeader.f_opthdr),std::ios_base::cur); + assert((size_t)getI16(xcoffHeader.f_opthdr) >= sizeof(aoutHeader)); + in.read((char*)&aoutHeader, sizeof(aoutHeader)); + in.seekg(getI16(xcoffHeader.f_opthdr) - sizeof(aoutHeader),std::ios_base::cur); if(verboseFlag) std::cerr << "flags: " << std::hex << getI16(xcoffHeader.f_flags) << std::dec << std::endl; @@ -184,11 +190,22 @@ void mkpef(std::istream& in, std::ostream& out, std::string mainSymbol = "__star xcoffSectionNames[i+1] = xcoffSection.s_name; } + PEFLoaderInfoHeader loaderInfoHeader; + memset(&loaderInfoHeader, 0, sizeof(loaderInfoHeader)); + + loaderInfoHeader.mainSection = -1; + loaderInfoHeader.initSection = -1; + loaderInfoHeader.termSection = -1; + + loaderInfoHeader.mainSection = 1; + loaderInfoHeader.mainOffset = getI32(aoutHeader.entry); + std::vector importLibs; std::vector relocInstructions; std::map importSources; - std::map importedSymbolIndices; + std::map importedSymbolIndicesByName; std::set importedSymbolSet; + std::vector importedSymbolIndices; int totalImportedSyms = 0; { external_scnhdr xcoffLoaderSection = xcoffSections[".loader"]; @@ -239,167 +256,67 @@ void mkpef(std::istream& in, std::ostream& out, std::string mainSymbol = "__star if(verboseFlag) std::cerr << "... from file: " << sym.l_ifile << std::endl; importLibs[sym.l_ifile].imports.push_back(name); + importLibs[sym.l_ifile].xcoffImportIndices.push_back(i); importSources[name] = totalImportedSyms; importedSymbolSet.insert(name); totalImportedSyms++; } } + importedSymbolIndices.resize(xcoffLoaderHeader.l_nsyms); { int symbolIndex = 0; for(unsigned i=1;i 4) - in.read(stringTable+4, stringTableLen-5); - stringTable[stringTableLen-1] = 0; - } - - if(verboseFlag) - { - std::cerr << "tell: " << in.tellg() << std::endl; - std::cerr << "seeking to symptr: " << getI32(xcoffHeader.f_symptr) << std::endl; - } - in.seekg(getI32(xcoffHeader.f_symptr),std::ios::beg); - - if(verboseFlag) - std::cerr << "tell: " << in.tellg() << std::endl; - - int nSymEntries = getI32(xcoffHeader.f_nsyms); - std::vector syms(nSymEntries); - in.read((char*) &syms[0], sizeof(external_syment)*nSymEntries); - - std::vector symNames; - - for(int i=0; i < nSymEntries; i++) - { - external_syment ent = syms[i]; - - std::string name; - if(getI32(ent.e.e.e_zeroes) == 0) + internal_ldrel rel = rels[i]; + eswap(&rel, "LLss"); + /* + bfd_vma l_vaddr; + bfd_size_type l_symndx; + short l_rtype; + short l_rsecnm; + */ + if(verboseFlag) { - if(getI16(ent.e_scnum) == -2) - name = "#debug#"; - else - name = stringTable + getI32(ent.e.e.e_offset); - + std::cerr << "reloc: " << std::hex << rel.l_vaddr << " " << rel.l_symndx << " " << rel.l_rtype << " " << rel.l_rsecnm << "\n"; } + assert(rel.l_rtype == 0x1f00); + assert(rel.l_rsecnm == xcoffDataSecNumber); + + int vaddr = rel.l_vaddr; + + relocInstructions.push_back( + PEFRelocComposeSetPosition_1st(vaddr)); + relocInstructions.push_back( + PEFRelocComposeSetPosition_2nd(vaddr)); + if(rel.l_symndx == 0) + relocInstructions.push_back(PEFRelocComposeBySectC(1)); + else if(rel.l_symndx == 1 || rel.l_symndx == 2) + relocInstructions.push_back(PEFRelocComposeBySectD(1)); else - name = ent.e.e_name; - if(verboseFlag) { - std::cerr << "[" << i << "] Symbol: " << name << std::hex - << " e_value: " << getI32(ent.e_value) - << " e_scnum: " << getI16(ent.e_scnum) - << " e_type: " << getI16(ent.e_type) - << " e_sclass: " << (int)ent.e_sclass[0] - << std::dec << std::endl; - } - symNames.push_back(name); - for(int j=0; j