Commit 5099dd8d authored by Julian Kranz's avatar Julian Kranz

---

parent c488bb68
.\"Modified from man(1) of FreeBSD, the NetBSD mdoc.template, and mdoc.samples.
.\"See Also:
.\"man mdoc.samples for a complete listing of options
.\"man mdoc for the short list of editing options
.\"/usr/share/misc/mdoc.template
.Dd 14.09.12 \" DATE
.Dt CFGresolve 1 \" Program name and manual section number
.Os Darwin
.Sh NAME \" Section Header - required - don't modify
.Nm CFGresolve,
.\" The following lines are read in generating the apropos(man -k) database. Use only key
.\" words here as the database is built based on the words here and in the .ND line.
.Nm Other_name_for_same_program(),
.Nm Yet another name for the same program.
.\" Use .Nm macro to designate other names for the documented program.
.Nd This line parsed for whatis database.
.Sh SYNOPSIS \" Section Header - required - don't modify
.Nm
.Op Fl abcd \" [-abcd]
.Op Fl a Ar path \" [-a path]
.Op Ar file \" [file]
.Op Ar \" [file ...]
.Ar arg0 \" Underlined argument - use .Ar anywhere to underline
arg2 ... \" Arguments
.Sh DESCRIPTION \" Section Header - required - don't modify
Use the .Nm macro to refer to your program throughout the man page like such:
.Nm
Underlining is accomplished with the .Ar macro like this:
.Ar underlined text .
.Pp \" Inserts a space
A list of items with descriptions:
.Bl -tag -width -indent \" Begins a tagged list
.It item a \" Each item preceded by .It macro
Description of item a
.It item b
Description of item b
.El \" Ends the list
.Pp
A list of flags and their descriptions:
.Bl -tag -width -indent \" Differs from above in tag removed
.It Fl a \"-a flag as a list item
Description of -a flag
.It Fl b
Description of -b flag
.El \" Ends the list
.Pp
.\" .Sh ENVIRONMENT \" May not be needed
.\" .Bl -tag -width "ENV_VAR_1" -indent \" ENV_VAR_1 is width of the string ENV_VAR_1
.\" .It Ev ENV_VAR_1
.\" Description of ENV_VAR_1
.\" .It Ev ENV_VAR_2
.\" Description of ENV_VAR_2
.\" .El
.Sh FILES \" File used or created by the topic of the man page
.Bl -tag -width "/Users/joeuser/Library/really_long_file_name" -compact
.It Pa /usr/share/file_name
FILE_1 description
.It Pa /Users/joeuser/Library/really_long_file_name
FILE_2 description
.El \" Ends the list
.\" .Sh DIAGNOSTICS \" May not be needed
.\" .Bl -diag
.\" .It Diagnostic Tag
.\" Diagnostic informtion here.
.\" .It Diagnostic Tag
.\" Diagnostic informtion here.
.\" .El
.Sh SEE ALSO
.\" List links in ascending order by section, alphabetically within a section.
.\" Please do not reference files that do not exist without filing a bug report
.Xr a 1 ,
.Xr b 1 ,
.Xr c 1 ,
.Xr a 2 ,
.Xr b 2 ,
.Xr a 3 ,
.Xr b 3
.\" .Sh BUGS \" Document known, unremedied bugs
.\" .Sh HISTORY \" Document history if command behaves in a unique manner
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
//
// 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.
//
#include <stdint.h>
#include <strings.h>
#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;
}
return o << std::setw(0) << std::dec;
}
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 ;
}
//
// Segment.h
//
// 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.
//
#ifndef __CFGresolve__Segment__
#define __CFGresolve__Segment__
#include <iostream>
#include <vector>
class Segment {
std::string name;
const uint64_t base;
const uint64_t size;
const uint64_t file_offset;
const uint64_t displ;
char* content;
public:
Segment(char* seg_name, uint64_t base, uint64_t size, uint64_t file_offset) :
base(base), size(size), file_offset(file_offset), displ(file_offset % getpagesize()) {
name.append(seg_name);
content = 0;
}
bool addressInRange(uint64_t addr);
char* getPtrAt(uint64_t addr, char** limit);
// Load segment into memory. Returns true if successful.
bool map(int fd);
std::string getName() {
return name;
}
uint64_t getBase() {
return base;
}
void unmap();
~Segment() {
unmap();
}
std::ostream& dump(std::ostream& o, uint64_t addr = 0, uint64_t len = 0);
friend std::ostream& operator<<(std::ostream& o, const Segment& s);
};
class SegmentSet : public std::vector<Segment*> {
public:
char* resolve(uint64_t addr, char** limit) const {
for (typename std::vector<Segment*>::const_iterator
iter = this->begin(); iter != this->end(); iter++) {
if ((*iter)->addressInRange(addr)) return (*iter)->getPtrAt(addr, limit);
}
if (limit) *limit=NULL;
return NULL;
}
void addSegment(Segment* seg) { push_back(seg); }
bool map(int fd) {
for (typename std::vector<Segment*>::const_iterator
iter = this->begin(); iter != this->end(); iter++) {
bool res = (*iter)->map(fd);
if (!res) return false;
};
return true;
}
std::ostream& dump(std::ostream& o) const {
for (typename std::vector<Segment*>::const_iterator
iter = this->begin(); iter != this->end(); iter++) {
(*iter)->dump(o);
}
return o;
};
Segment* findByName(const std::string segName) const {
for (typename std::vector<Segment*>::const_iterator
iter = this->begin(); iter != this->end(); iter++) {
if ((*iter)->getName().find(segName)!=std::string::npos) return *iter;
}
return NULL;
};
friend std::ostream& operator<<(std::ostream& o, const SegmentSet& s);
};
#endif /* defined(__CFGresolve__Segment__) */
/*
* main-elf.cpp
*
* Created on: Oct 23, 2012
* Author: jucs
*/
#include <stdint.h>
#include <iostream>
#include <fstream>
#include "Segment.h"
#include "RReil.h"
#include <vector>
#include <fcntl.h>
int main(int argc, const char * argv[]) {
char *file_name = "/bin/echo";
SegmentSet segments;
// Read in what code/data segments there are and store them in
// uint32_t cmd_buf[header.sizeofcmds/sizeof(uint32_t)];
// char* cp = (char*) &cmd_buf;
// f.read(cp, header.sizeofcmds);
// uint32_t ncmds = header.ncmds;
// while ((cp-(char*) &cmd_buf)<header.sizeofcmds && ncmds--) {
// struct load_command* lc = (struct load_command*) cp;
// switch (lc->cmd) {
// case LC_SEGMENT_64: {
// struct segment_command_64* cmd = (struct segment_command_64*) cp;
// char name[17];
// name[16]=0;
// memcpy(&name[0], &cmd->segname[0], 16);
// std::cout << "found " << &name[0] << " segment" << std::endl;
// uint32_t nsect = cmd->nsects;
// struct section_64* sect = (struct section_64*) (cp+sizeof(struct segment_command_64));
// for (int i=0; i<nsect; i++, sect++) {
// sect->sectname[15]=0;
// Segment* s = new Segment(&(sect->sectname[0]), sect->addr, sect->size, sect->offset);
// segments.addSegment(s);
// }
// }
// }
// cp = cp+lc->cmdsize;
// }
Segment* s = new Segment("text", 0x401170, 0xbd, 0x1170);
segments.addSegment(s);
// f.close();
int fd = open(file_name, O_RDONLY);
if (fd== -1) return 1;
bool res = segments.map(fd);
if (!res) {
std::cout << "Cannot map segment sections." << std::endl;
return 1;
}
//std::cout << segments;
Segment* text = segments.findByName("text");
if (text==NULL) {
std::cout << "Segment containing code not found." << std::endl;
return 1;
}
std::cout << segments << std::endl;
uint64_t start = text->getBase();
char* limit;
char* addr = segments.resolve(start, &limit);
//std::cout << "first byte of text segment is " << std::hex << start << ": " << std::hex << (uint8_t) (*addr) << std::dec << std::endl;
RReilBB* b = translate(addr,limit);
if (b!=NULL) std::cout << "basic block:" << std::endl << *b << std::endl;
return 0;
}
//
// main.cpp
// CFGresolve
//
// Created by Axel Simon on 14.09.12.
// Copyright (c) 2012 Axel Simon. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <mach-o/loader.h>
#include "Segment.h"
#include "RReil.h"
#include <vector>
#include <fcntl.h>
int main(int argc, const char * argv[]) {
if (argc!=2) {
std::cout << "Please specify executable as argument." << std::endl;
return 1;
}
const char* fileName = argv[1];
std::cout << "opening " << fileName << std::endl;
std::ifstream f(fileName);
struct mach_header_64 header;
f.read((char*) &header, sizeof(header));
if (header.magic!=MH_MAGIC_64 || header.filetype!=MH_EXECUTE) {
std::cout << "Not an executable." << std::endl;
return 1;
}
if (header.cputype!=CPU_TYPE_X86_64) {
std::cout << "Not an x86 64-bit executable." << std::endl;
return 1;
}
SegmentSet segments;
// Read in what code/data segments there are and store them in
uint32_t cmd_buf[header.sizeofcmds/sizeof(uint32_t)];
char* cp = (char*) &cmd_buf;
f.read(cp, header.sizeofcmds);
uint32_t ncmds = header.ncmds;
while ((cp-(char*) &cmd_buf)<header.sizeofcmds && ncmds--) {
struct load_command* lc = (struct load_command*) cp;
switch (lc->cmd) {
case LC_SEGMENT_64: {
struct segment_command_64* cmd = (struct segment_command_64*) cp;
char name[17];
name[16]=0;
memcpy(&name[0], &cmd->segname[0], 16);
std::cout << "found " << &name[0] << " segment" << std::endl;
uint32_t nsect = cmd->nsects;
struct section_64* sect = (struct section_64*) (cp+sizeof(struct segment_command_64));
for (int i=0; i<nsect; i++, sect++) {
sect->sectname[15]=0;
Segment* s = new Segment(&(sect->sectname[0]), sect->addr, sect->size, sect->offset);
segments.addSegment(s);
}
}
}
cp = cp+lc->cmdsize;
}
f.close();
int fd = open(fileName, O_RDONLY);
if (fd== -1) return 1;
bool res = segments.map(fd);
if (!res) {
std::cout << "Cannot map segment sections." << std::endl;
return 1;
}
//std::cout << segments;
Segment* text = segments.findByName("text");
if (text==NULL) {
std::cout << "Segment containing code not found." << std::endl;
return 1;
}
// std::cout << "text segment is:\n";
// text->dump(std::cout);
// std::cout << std::endl;
uint64_t start = text->getBase();
char* limit;
RReilAddress addr = segments.resolve(start, &limit);
while (true) {
//std::cout << "first byte of text segment is " << std::hex << start << ": " << std::hex << (uint8_t) (*addr) << std::dec << std::endl;
RReilBB* b = translate(addr,limit);
if (b==NULL) break;
std::cout << "basic block:" << std::endl << *b << std::endl;
};
return 0;
}
# tests that both branches of an if-statement are flowing into the result
val ite c t e = if c then t else e
val res = $bar (ite '1' {bar=1,sum=7} {sum=8,zoo=9})
# tests that both branches of an if-statement are flowing into the result
val ite c t e = if c then t else e
val res = $sum (ite '1' {bar=1,sum=7} {sum=8,zoo=9})
# tests that both branches of an if-statement are flowing into the result
val foo = if '1' then {bar=1,sum=7} else {sum=8,zoo=9}
val bar = $zoo foo
# tests that both branches of an if-statement are flowing into the result
val foo = if '1' then {bar=1,sum=7} else {sum=8,zoo=9}
val bar = $sum foo
# applying a function to the wrong number of arguments
val f x y z = { sum = x + y, diff = x - z}
val res x = f x 7
# applying a function to the wrong number of arguments
val f x y z = { sum = x + y, diff = x - z}
val res x = f x 7 x
# tests checking of no of arguments of recursive calls
val f x y z = if x then f '0' z else 7
# tests checking of no of arguments of recursive calls
val f x y z = if x then f '0' z y else 7
# tests if unknown definitions are checked correctly
val res = $foobar (id {bar=7, foo="Hi", baz='1'})
val id x = x
# tests if unknown definitions are checked correctly
val res = $foo (id {bar=7, foo="Hi", baz='1'})
val id x = x
# test record field
val r = { foo = "a string", bar = 7 }
val s = $baz r
# test record field
val r = { foo = "a string", bar = 7 }
val s = $foo r
# test if fields flow properly in monadic results
val act = do
y <- return {num=7};
z <- return {add=8};
return (@{ add = $add z} y)
end
val res = do
r <- act;
return ($number r + $add r)
end
\ No newline at end of file
# test if fields flow properly in monadic results
val act = do
y <- return {num=7};
z <- return {add=8};
return (@{ add = $add z} y)
end
val res = do
r <- act;
return ($num r + $add r)
end
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment