gaulthiergain-tools/srcs/binarytool/elf64analyser/elf_pages.go
Gaulthier Gain f25637cdb7 Add binary analyser code to the toolchain
This commit adds the binary analyser code to the toolchain. This
binary analyser adds various features such as gathering ELF
information (e.g., functions, symbols, ...), micro-libs inspection,
ELF comparison, ELF pages division, ... Note that it was developped
with Unikraft structure in mind.

The binary analyser can be used as an external tool with the
'--binary' argument. A specific json file should be provided which
contains specific fields (see 'bin_analysis_example.json'). Please,
see the wiki for further information.

Signed-off-by: Gaulthier Gain <gaulthier.gain@uliege.be>
2021-01-30 12:37:27 +01:00

124 lines
2.5 KiB
Go

// Copyright 2019 The UNICORE Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file
//
// Author: Gaulthier Gain <gaulthier.gain@uliege.be>
package elf64analyser
import (
"fmt"
"io"
"os"
"strings"
)
const pageSize = 0x1000
type ElfFileSegment struct {
Filename string
NbPages int
Pages []*ElfPage
}
type ElfPage struct {
number int
startAddress uint64
contentByteArray []byte
hash string
libName string
sectionName string
noNullValues int
cap int
}
func (p *ElfPage) pageContentToString() string {
var builder strings.Builder
for i, entry := range p.contentByteArray {
if i > 0 && i%4 == 0 {
builder.WriteString(" ")
}
if i > 0 && i%16 == 0 {
builder.WriteString("\n")
}
_, _ = builder.WriteString(fmt.Sprintf("%02x", entry))
}
_, _ = builder.WriteString("")
return builder.String()
}
func (p *ElfPage) displayPageContent(mw io.Writer) {
/*
hexStartAddr, err := strconv.ParseInt(p.startAddress, 16, 64);
if err != nil {
panic(err)
}
*/
for i, entry := range p.contentByteArray {
if i > 0 && i%4 == 0 {
_, _ = fmt.Fprintf(mw, " ")
}
if i > 0 && i%16 == 0 {
_, _ = fmt.Fprintf(mw, "\n")
}
_, _ = fmt.Fprintf(mw, "%02x", entry)
}
_, _ = fmt.Fprintln(mw, "")
}
func (p *ElfPage) displayPageContentShort(mw io.Writer) {
entryLine := 0
for i, entry := range p.contentByteArray {
if entry > 0 {
_, _ = fmt.Fprintf(mw, "[%d] %02x ", i, entry)
if entryLine > 0 && entryLine%16 == 0 {
_, _ = fmt.Fprintf(mw, "\n")
}
entryLine++
}
}
_, _ = fmt.Fprintln(mw, "")
}
func SavePagesToFile(pageTables []*ElfPage, filename string, shortView bool) error {
mw := io.MultiWriter(os.Stdout)
if len(filename) > 0 {
file, err := os.Create(filename)
if err != nil {
return err
}
mw = io.MultiWriter(file)
}
for i, p := range pageTables {
_, _ = fmt.Fprintln(mw, "----------------------------------------------------")
_, _ = fmt.Fprintf(mw, "Page: %d\n", i+1)
_, _ = fmt.Fprintf(mw, "LibName: %s\n", p.libName)
_, _ = fmt.Fprintf(mw, "Section: %s\n", p.sectionName)
_, _ = fmt.Fprintf(mw, "StartAddr: %x (%d)\n", p.startAddress, p.startAddress)
_, _ = fmt.Fprintf(mw, "Non-Null value: %d\n", p.noNullValues)
_, _ = fmt.Fprintf(mw, "Hash: %s\n", p.hash)
if shortView {
p.displayPageContentShort(mw)
} else {
p.displayPageContent(mw)
}
_, _ = fmt.Fprintln(mw, "----------------------------------------------------")
}
return nil
}