gaulthiergain-tools/srcs/binarytool/elf64core/elf_file.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

82 lines
1.6 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 elf64core
import (
"bufio"
"encoding/binary"
"os"
)
type ELF64File struct {
Header *ELF64Header
SectionsTable SectionsTable
SegmentsTable ProgramTable
DynamicTable DynamicTable
SymbolsTables []SymbolsTables
RelaTables []RelaTables
NotesTables []NotesTables
FunctionsTables []FunctionTables
Raw []byte
IndexSections map[string]int
Name string
Endianness binary.ByteOrder
TextSectionIndex []int // slice since we can have several
}
func (elfFile *ELF64File) ReadElfBinaryFile(filename string) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
stats, statsErr := file.Stat()
if statsErr != nil {
return statsErr
}
var size = stats.Size()
elfFile.Raw = make([]byte, size)
buf := bufio.NewReader(file)
_, err = buf.Read(elfFile.Raw)
return err
}
func (elfFile *ELF64File) ParseAll(path, name string) error {
elfFile.Name = name
if err := elfFile.ReadElfBinaryFile(path + name); err != nil {
return err
}
if err := elfFile.ParseElfHeader(); err != nil {
return err
}
if err := elfFile.ParseSectionHeaders(); err != nil {
return err
}
if err := elfFile.ParseSections(); err != nil {
return err
}
if err := elfFile.ParseProgramHeaders(); err != nil {
return err
}
if err := elfFile.parseFunctions(); err != nil {
return err
}
return nil
}