Package org.jakstab.asm

Examples of org.jakstab.asm.AbsoluteAddress


        //logger.debug("  Symbol entry: " + symbolTable[i]);
      }
      if (addressOfMain < 0) logger.warn("No main function found in object file!");

    }
    entryPoint = new AbsoluteAddress(imageBase + addressOfMain);
    logger.debug("Main function at " + entryPoint);
   
    /////////////////////////////////////////////////////////////////
    // Parse and perform relocations
    SectionHeader csHead = getSectionHeader(codeSection);
    relocations = new RelocationEntry[csHead.NumberOfRelocations];
    unresolvedSymbols = new FastSet<UnresolvedSymbol>();
    if (csHead.NumberOfRelocations > 0) {
      logger.debug("Relocations: ");
      inBuf.seek(csHead.PointerToRelocations);
      for (int i = 0; i < csHead.NumberOfRelocations; i++) {
        relocations[i] = new RelocationEntry(inBuf);
       
        SymbolEntry symbolEntry = symbolTable[relocations[i].getTableIndex()];
       
        logger.debug("  RVA 0x" + Integer.toHexString((int)(relocations[i].getRVA() + csHead.VirtualAddress)) + ": make " +
            (relocations[i].isDirectVirtualAddress() ? "direct" : "relative") +
            " to " + symbolEntry);
       
        // Mark relocation entries to external symbols as unresolved, so
        // the resolution mechanism in the Program class takes care of them
        UnresolvedSymbol.AddressingType mode;
        if (relocations[i].isDirectVirtualAddress()) mode = AddressingType.ABSOLUTE;
        else if (relocations[i].isRelativeDisplacement()) mode = AddressingType.PC_RELATIVE;
        else throw new RuntimeException("Unknown addressing type for unresolved symbol " + relocations[i].toString());

        String name = symbolEntry.getName();
        name = stripSymbolName(name);

        UnresolvedSymbol unresolvedSymbol = new UnresolvedSymbol(
            this,
            name,
            (int)(getFilePointerFromRVA(relocations[i].getRVA() + csHead.VirtualAddress)),
            mode
        );
        // If it's an external symbol, rely on the Program class to resolve it
        if (symbolEntry.isExternal()) {
          logger.debug("  -- Marking as external reference");
          unresolvedSymbols.add(unresolvedSymbol);
        } else {
          // Otherwise, perform relocation now
          logger.debug("  -- Relocating " + symbolEntry.getName());
          AbsoluteAddress relocatedAddress = new AbsoluteAddress(
              symbolEntry.getValue() + imageBase +
              section_headers[symbolEntry.getSectionNumber() - 1].VirtualAddress
              );
          logger.debug("  New address: " + relocatedAddress);
          unresolvedSymbol.resolve(relocatedAddress);
View Full Code Here


        //logger.debug("Locating symbol " + name);
       
        name = stripSymbolName(name);
        long fp = getSectionHeader(section).PointerToRawData + symbolTable[i].getValue();
       
        AbsoluteAddress address = getVirtualAddress(fp);
        exportedSymbols.add(new ExportedSymbol(this, name, address));
        logger.debug("Exporting " + name + " at file offset " + fp + ", section offset " +
            symbolTable[i].getValue() + " in " + section_headers[section].getName() + ", which evaluates to VA " + address);

      }
View Full Code Here

      int eatEntries = 0;
      for (int i = 0; i < tmpEntries.length; i++) {
        long rva = inBuf.readDWORD();
        if (rva > 0) {
          tmpEntries[i] = new ExportEntry((int)(i + imageExportDirectory.Base),
              new AbsoluteAddress(rva + getBaseAddress()));
          eatEntries++;
        }
      }
     
      long namePtr = getFilePointerFromRVA(imageExportDirectory.AddressOfNames);
      long ordPtr = getFilePointerFromRVA(imageExportDirectory.AddressOfNameOrdinals);
      for (int i = 0; i < imageExportDirectory.NumberOfNames; i++) {
        // read next ENT entry
        inBuf.seek(namePtr);
        long rva = inBuf.readDWORD();
        namePtr = inBuf.getCurrent();
        // read export name
        inBuf.seek(getFilePointerFromRVA(rva));
        String expName = inBuf.readASCII();
        // read next EOT entry
        inBuf.seek(ordPtr);
        int ord = inBuf.readWORD();
        ordPtr = inBuf.getCurrent();
        tmpEntries[ord].setName(expName);
      }
      exportEntries = new ExportEntry[eatEntries];
      int j = 0;
      for (int i = 0; i < tmpEntries.length; i++)
        if (tmpEntries[i] != null) exportEntries[j++] = tmpEntries[i];
      logger.debug("-- Got " + exportEntries.length + " exported symbols.");
    } else logger.debug("-- File contains no exports");
   
   
    /////////////////////////////////////////////////////////////////
    // Parse imports and build import table
    importTable = new HashMap<AbsoluteAddress, Pair<String,String>>();
    long impTableRVA =
      pe_header.getDataDirectory()[ImageDataDirectory.IMPORT_TABLE_INDEX].VirtualAddress;
    if (impTableRVA > 0) { // We have an import table
      logger.debug("-- Reading image import descriptors...");
      inBuf.seek(getFilePointerFromRVA(impTableRVA));
      List<ImageImportDescriptor> imageImportDescriptors =
        new LinkedList<ImageImportDescriptor>();
      while(true) {
        ImageImportDescriptor cur = new ImageImportDescriptor(inBuf);
        if (cur.isZero()) break;
        imageImportDescriptors.add(cur);
      }

      for (ImageImportDescriptor descriptor : imageImportDescriptors) {
        inBuf.seek(getFilePointerFromRVA(descriptor.Name));
        String libraryFileName = inBuf.readASCII();
        logger.debug("-- Parsing imports from " + libraryFileName + "...");
        // Normalize filenames to lower case
        libraryFileName = libraryFileName.toLowerCase();

        // Check if the library is bound.
        boolean bound = descriptor.TimeDateStamp != 0;

        /* Read Import Address Table or Import Name Table */
        long iatFilePtr;
        if (bound) iatFilePtr = getFilePointerFromRVA(descriptor.OriginalFirstThunk);
        else iatFilePtr = getFilePointerFromRVA(descriptor.FirstThunk);
        // import names will be associated to IAT addresses in any case
        //AbsoluteAddress iatAddress = (new RVAPointer(this, descriptor.FirstThunk)).getVAPointer();
        AbsoluteAddress iatAddress = new AbsoluteAddress(descriptor.FirstThunk + getBaseAddress());

        while(true) {
          inBuf.seek(iatFilePtr);
          long thunk = inBuf.readDWORD();
          iatFilePtr = inBuf.getCurrent(); // Save buffer position
          if (thunk == 0) break;
          if ((thunk & 0x80000000) != 0) {
            /* Thunk contains ordinal value in low 31 bits.
             * (for 64 bit files this would be the lower 63 bits.  */
            int ord = (int) (thunk & 0x7FFFFFFF);
            String ordName = "ord(" + ord + ")";
            importTable.put(iatAddress,  Pair.create(libraryFileName, ordName));
          } else {
            /* Thunk contains an RVA of either a IMAGE_IMPORT_BY_NAME
             * structure [word (ord hint) , string (function name)]
             * or to a forwarder string. Forwarding not supported at
             * the moment!*/

            long rva = getFilePointerFromRVA(thunk);
            if (rva < 0)
              throw new BinaryParseException("RVA in thunk points outside of image!");
            // Just skip the ord hint (WORD), we don't need it.
            inBuf.seek(rva + 2);
            String funcName = inBuf.readASCII();
            importTable.put(iatAddress, Pair.create(libraryFileName, funcName));
          }
          // Advance IAT entry by one DWORD
          iatAddress = new AbsoluteAddress(iatAddress.getValue() + 4);
        }
      }
    }
   
   
View Full Code Here

    return exportEntries[num];
  }

  @Override
  public AbsoluteAddress getEntryPoint() {
    return new AbsoluteAddress(getBaseAddress() + pe_header.getAddressOfEntryPoint());
  }
View Full Code Here

  @Override
  public Set<UnresolvedSymbol> getUnresolvedSymbols() {
    Set<UnresolvedSymbol> unresolvedSymbols = new FastSet<UnresolvedSymbol>();
    for (Map.Entry<AbsoluteAddress, Pair<String, String>> importEntry : getImportTable().entrySet()) {
      AbsoluteAddress va = importEntry.getKey();
      String libraryName = importEntry.getValue().getLeft();
      String symbolName = importEntry.getValue().getRight();
      unresolvedSymbols.add(new UnresolvedSymbol(this, libraryName, symbolName, (int)getFilePointer(va), AddressingType.ABSOLUTE));
    }
   
View Full Code Here

  private final AbsoluteAddress cur;
  private final SetMultimap<AbsoluteAddress,AbsoluteAddress> succ;
 
  private TraceReplayState() {
    super();
    cur = new AbsoluteAddress(0xF0000B07L);
    succ = null;
  }
View Full Code Here

    }

    // Read entire trace
   
    String line = null;
    AbsoluteAddress curPC = null;
    AbsoluteAddress lastPC = null;
   
    do {
      String lastLine = line;
      try {
        line = in.readLine();
      } catch (IOException e) {
        logger.fatal("IO error when reading from trace: " + e.getMessage());
        throw new RuntimeException(e);
      }
      if (line != null) {
       
        if (line.charAt(0) == 'A') {
          // Dima's "parsed" format
          curPC = new AbsoluteAddress(Long.parseLong(line.substring(9, line.indexOf('\t', 9)), 16));
        } else {
          // Pure format produced by temu's text conversion
          curPC = new AbsoluteAddress(Long.parseLong(line.substring(0, line.indexOf(':')), 16));
        }
       
        if (line.equals(lastLine)) {
          //logger.warn("Warning: Skipping duplicate line in trace for address " + curPC);
        } else {
View Full Code Here

   * @param fp the file pointer
   * @return the virtual address
   */
  public final AbsoluteAddress getVirtualAddress(long fp) {
    long rva = getRVAFromFilePointer(fp);
    if (rva >= 0) return new AbsoluteAddress(rva + getBaseAddress());
    else return null;
  }
View Full Code Here

    for (int i=0; i < getNumberOfSections(); i++) {
      highAddress = Math.max(getSectionHeader(i).VirtualAddress +
          getSectionHeader(i).SizeOfRawData, highAddress);
    }
    highAddress += getBaseAddress();
    return new AbsoluteAddress(highAddress);
  }
View Full Code Here

    long lowAddress = Long.MAX_VALUE;
    for (int i=0; i < getNumberOfSections(); i++) {
      lowAddress = Math.min(getSectionHeader(i).VirtualAddress, lowAddress);
    }
    lowAddress += getBaseAddress();
    return new AbsoluteAddress(lowAddress);
  }
View Full Code Here

TOP

Related Classes of org.jakstab.asm.AbsoluteAddress

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.