Review stats (use virtual size instead of ELF file)

This commit is contained in:
Gaulthier Gain 2021-12-05 21:19:38 +01:00
parent 6e5d61f946
commit 134f6eb566
2 changed files with 49 additions and 15 deletions

View file

@ -9,7 +9,9 @@ package elf64analyser
import ( import (
"errors" "errors"
"fmt" "fmt"
"math"
"os" "os"
"sort"
"strconv" "strconv"
"strings" "strings"
"text/tabwriter" "text/tabwriter"
@ -207,21 +209,52 @@ func (analyser *ElfAnalyser) DisplayStatSize(elfFile *elf64core.ELF64File) {
var totalSizeText uint64 var totalSizeText uint64
var totalSizeElf uint64 var totalSizeElf uint64
_, _ = fmt.Fprintf(w, "Name\tFile size (Bytes/Hex)\n") _, _ = fmt.Fprintf(w, "-----------------------------------------------------------------------\n")
for _, s := range elfFile.SectionsTable.DataSect { _, _ = fmt.Fprintf(w, "Name\tVirtual Size (Bytes/Hex) \t#pages\tInfos:\n")
if len(s.Name) > 0 {
_, _ = fmt.Fprintf(w, "%s\t%d (0x%x)\n", s.Name, s.Elf64section.Size, s.Elf64section.Size) // Sort by addresses
dataSec := elfFile.SectionsTable.DataSect
sort.Slice(dataSec, func(i, j int) bool {
return dataSec[i].Elf64section.VirtualAddress < dataSec[j].Elf64section.VirtualAddress
})
for i, s := range elfFile.SectionsTable.DataSect { //&uint64(elf.SHF_WRITE)
if s.Elf64section.VirtualAddress > 0 {
var size uint64
var currNext = ""
if strings.Contains(s.Name, elf64core.IntrstackSection) || strings.Contains(s.Name, elf64core.TbssSection) {
size = s.Elf64section.Size
totalSizeElf += size
} else {
size = dataSec[i+1].Elf64section.VirtualAddress -
dataSec[i].Elf64section.VirtualAddress
totalSizeElf += size
currNext = fmt.Sprintf("0x%x -> 0x%x : (%s)-> (%s)", dataSec[i].Elf64section.VirtualAddress,
dataSec[i+1].Elf64section.VirtualAddress, dataSec[i].Name, dataSec[i+1].Name)
}
if strings.Contains(s.Name, elf64core.TextSection) && strings.Contains(dataSec[i+1].Name, elf64core.TextSection) {
totalSizeText += size
} else if strings.Contains(s.Name, elf64core.TextSection) {
// Main application code
size = s.Elf64section.Size
totalSizeText += size
}
_, _ = fmt.Fprintf(w, "%s\t%d (0x%x)\t%.2f\t%s\n", s.Name, size, size, float32(size)/float32(pageSize), currNext)
} }
if strings.Contains(s.Name, elf64core.TextSection) {
totalSizeText += s.Elf64section.Size
}
totalSizeElf += s.Elf64section.Size
} }
_, _ = fmt.Fprintf(w, "----------------------\t----------------------\n") _, _ = fmt.Fprintf(w, "----------------------\t----------------------\t------\t----------------------------\n")
_, _ = fmt.Fprintf(w, "Total Size:\n") _, _ = fmt.Fprintf(w, "Total Size:\n")
_, _ = fmt.Fprintf(w, "Section .text:\t%d (0x%x)\n", totalSizeText, totalSizeText) _, _ = fmt.Fprintf(w, "Section .text:\t%d (0x%x)\n", totalSizeText, totalSizeText)
_, _ = fmt.Fprintf(w, "All sections:\t%d (0x%x)\n", totalSizeElf, totalSizeElf) _, _ = fmt.Fprintf(w, "All sections:\t%d (0x%x)\n", totalSizeElf, totalSizeElf)
_, _ = fmt.Fprintf(w, "#Pages (.text):\t%d\n", totalSizeText/pageSize) _, _ = fmt.Fprintf(w, "#Pages (.text):\t%d\n", roundPage(float64(totalSizeText)/float64(pageSize)))
_, _ = fmt.Fprintf(w, "#Pages (all sections):\t%d\n", totalSizeElf/pageSize) _, _ = fmt.Fprintf(w, "#Pages (all sections):\t%d\n", roundPage(float64(totalSizeElf)/float64(pageSize)))
_ = w.Flush() _ = w.Flush()
} }
func roundPage(x float64) uint64 {
return uint64(math.Round(x + 0.5))
}

View file

@ -7,10 +7,11 @@
package elf64core package elf64core
const ( const (
TextSection = ".text" TextSection = ".text"
BssSection = ".bss" BssSection = ".bss"
DataSection = ".data" TbssSection = ".tbss"
RodataSection = ".rodata" DataSection = ".data"
RodataSection = ".rodata"
IntrstackSection = ".intrstack" IntrstackSection = ".intrstack"
BootTextSection = ".text.boot" BootTextSection = ".text.boot"