Segment.cpp 2.36 KB
Newer Older
Axel Simon's avatar
Axel Simon committed
1 2 3 4 5 6 7 8 9 10 11
//
//  Segment.cpp
//
//  A segment is a blob of code or data in the program.
//
//  CFGresolve
//
//  Created by Axel Simon on 15.09.12.
//  Copyright (c) 2012 Axel Simon. All rights reserved.
//

Julian Kranz's avatar
Julian Kranz committed
12
#include <stdint.h>
Julian Kranz's avatar
Julian Kranz committed
13
#include <strings.h>
Axel Simon's avatar
Axel Simon committed
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
#include "Segment.h"
#include <sys/mman.h>
#include <unistd.h>
#include <errno.h>
#include <err.h>
#include <iomanip>

bool Segment::addressInRange(uint64_t addr) {
  return (addr>=base && addr<=base+size);
};

char* Segment::getPtrAt(uint64_t addr, char** limit) {
  if (limit) *limit = content+size;
  return content+(addr-base);
};

std::ostream& operator<<(std::ostream& o, const Segment& s) {
  return o << s.name << "[" << std::hex << s.base << "," << s.size << "]";
};

bool Segment::map(int fd) {
  void* res;
  if (file_offset==0) {
    // this segement is not backed by a file but a zero-filled one
    res = mmap(NULL, size+displ, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON, 0, 0);
    if (res!=MAP_FAILED) bzero(res,size+displ);
  } else {
    res = mmap(NULL, size+displ, PROT_READ, MAP_SHARED, fd, file_offset-displ);
  }
  if (res==MAP_FAILED) {
    err(errno, "when mapping section");
    return false;
  }
  content = ((char*) res)+displ;
  return true;
}

void Segment::unmap() {
  if (content) {
    munmap(content-displ, size);
    content = NULL;
  }
}

std::ostream& Segment::dump(std::ostream& o, uint64_t addr, uint64_t len) {
  if (addr==0) addr=base;
  if (len==0) len=size;
  if (!addressInRange(addr))
    return o << "Segment.dump: address " << std::hex << addr << " out of range" << std::endl;
  if (len>size)
    return o << "Segment.dump: size too large" << std::endl;
  char txt[17];
  txt[16]=0;
  for (uint64_t i=addr-base; i<len; i+=16) {
    o << std::hex << base+i << ": ";
    for (uint64_t j=0; j<16; j++) {
      if (i+j>=len) {
        o << "   ";
        txt[j] = ' ';
      } else {
        char c = content[i+j];
        o.precision(2);
        o << std::setfill('0') << std::setw(2) << std::hex << (uint32_t) ((unsigned char) c) << " ";
        txt[j] = (c>=32 && c<125 ? c : '.');
      }
    }
    o << "  " << &(txt[0]) << std::endl;
  }
82
  return o << std::setw(0) << std::dec;
Axel Simon's avatar
Axel Simon committed
83 84 85 86 87 88 89 90 91 92 93
}

std::ostream& operator<<(std::ostream& o, const SegmentSet& s) {
  for (typename std::vector<Segment*>::const_iterator
       iter = s.begin(); iter != s.end(); iter++) {
    o << *(*iter) << std::endl;
    (*iter)->dump(o);
  }
  return o ;
}