Skip to content
This repository was archived by the owner on Mar 8, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: QA
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v5
with:
go-version: '1.21'
cache: false
- uses: golangci/golangci-lint-action@v3
with:
version: v1.55
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-go@v5
with:
go-version: '1.21'
- name: Tests
run: go test -covermode=atomic -coverprofile=.coverage.out ./...
- uses: sonarsource/sonarcloud-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
with:
args: >
-Dsonar.organization=zoftko
-Dsonar.projectKey=zoftko_felf-cli
-Dsonar.test.inclusions=**/*_test.go
-Dsonar.go.coverage.reportPaths=.coverage.out
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/zoftko/felf-cli

go 1.21
73 changes: 73 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"debug/elf"
"flag"
"log"
)

const (
CODE SectionCategory = "code"
DATA SectionCategory = "data"
BSS SectionCategory = "bss"
UNKNOWN SectionCategory = "unknown"
)

type Measurements struct {
textSize uint64
dataSize uint64
bssSize uint64
}

type SectionCategory string

func main() { cli() }

func cli() Measurements {
flag.Parse()
args := flag.Args()
if len(args) != 1 {
log.Fatal("Only a single positional argument is supported")
}

file, err := elf.Open(args[0])
if err != nil {
log.Fatalf(err.Error())
}

result := Measurements{}
for _, section := range file.Sections {
if section.Type == elf.SHT_NULL {
continue
}

switch sectionCategory(section) {
case CODE:
result.textSize += section.Size
case DATA:
result.dataSize += section.Size
case BSS:
result.bssSize += section.Size
}
}

return result
}

func sectionCategory(section *elf.Section) SectionCategory {
if (section.Flags & elf.SHF_ALLOC) == 0 {
return UNKNOWN
}

if section.Type == elf.SHT_PROGBITS {
if (section.Flags & elf.SHF_WRITE) == 0 {
return CODE
} else {
return DATA
}
} else if section.Type == elf.SHT_NOBITS {
return BSS
}

return UNKNOWN
}
34 changes: 34 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package main

import (
"os"
"testing"
)

func compareMeasurements(t *testing.T, expect, got Measurements) {
if expect.textSize != got.textSize {
t.Errorf("expected text size %d got %d", expect.textSize, got.textSize)
}
if expect.dataSize != got.dataSize {
t.Errorf("expected data size %d got %d", expect.dataSize, got.dataSize)
}
if expect.bssSize != got.bssSize {
t.Errorf("expected bss size %d got %d", expect.bssSize, got.bssSize)
}
}

func TestCliMeasurements(t *testing.T) {
defer func(old []string) { os.Args = old }(os.Args)
os.Args = []string{"cmd", "testdata/rgctl.elf"}

result := cli()
compareMeasurements(
t,
Measurements{
textSize: 940,
dataSize: 2,
bssSize: 6,
},
result,
)
}
Binary file added testdata/rgctl.elf
Binary file not shown.