Skip to content

Commit

Permalink
wip: generate linker sections
Browse files Browse the repository at this point in the history
  • Loading branch information
m-gorecki committed Feb 19, 2024
1 parent 07e635e commit c5566f0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
33 changes: 33 additions & 0 deletions newt/builder/extcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,39 @@ func (t *TargetBuilder) execExtCmds(sf stage.StageFunc, userSrcDir string,
return nil
}

func getSectionEntry(name string, dest string) string {
entry := "." + name + " : \n" + "{ \n\t" + "__" + name + "_start__ = .;\n\t" +
"*(." + name + ")\n\t" +
"__" + name + "_end__ = .;\n" +
"} > " + dest + "\n\n"

return entry
}

func (t *TargetBuilder) generateSectionHeaders() {
var s []string

for _, pkg := range t.res.LpkgRpkgMap {
s = append(s, pkg.Lpkg.Sections()...)
}

if len(s) == 0 {
return
}

ram_header, err := os.Create(GeneratedIncludeDir(t.target.FullName()) + "/ld_ram.h")
text_header, err := os.Create(GeneratedIncludeDir(t.target.FullName()) + "/ld_text.h")
if err != nil {
return
}

for _, section := range s {
ram_header.WriteString(getSectionEntry(section, "RAM"))
text_header.WriteString(getSectionEntry(section, "TEXT"))
}

}

// execPreBuildCmds runs the target's set of pre-build user commands. It is an
// error if any command fails (exits with a nonzero status).
func (t *TargetBuilder) execPreBuildCmds(workDir string) error {
Expand Down
2 changes: 2 additions & 0 deletions newt/builder/targetbuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,8 @@ func (t *TargetBuilder) Build() error {
os.RemoveAll(workDir)
}()

t.generateSectionHeaders()

// Execute the set of pre-build user scripts.
if err := t.execPreBuildCmds(workDir); err != nil {
return err
Expand Down
17 changes: 17 additions & 0 deletions newt/pkg/localpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ type LocalPackage struct {
// General information about the package
desc *PackageDesc

sections []string

// Extra package-specific settings that don't come from syscfg. For
// example, SELFTEST gets set when the newt test command is used.
injectedSettings *cfgv.Settings
Expand Down Expand Up @@ -145,6 +147,10 @@ func (pkg *LocalPackage) Desc() *PackageDesc {
return pkg.desc
}

func (pkg *LocalPackage) Sections() []string {
return pkg.sections
}

func (pkg *LocalPackage) SetName(name string) {
pkg.name = name
}
Expand Down Expand Up @@ -197,6 +203,15 @@ func (pkg *LocalPackage) readDesc(yc ycfg.YCfg) (*PackageDesc, error) {
return pdesc, nil
}

func (pkg *LocalPackage) readSections(yc ycfg.YCfg) ([]string, error) {
var err error

sections, err := yc.GetValStringSlice("pkg.sections", nil)
util.OneTimeWarningError(err)

return sections, nil
}

func (pkg *LocalPackage) sequenceString(key string) string {
var buffer bytes.Buffer

Expand Down Expand Up @@ -361,6 +376,8 @@ func (pkg *LocalPackage) Load() error {
return err
}

pkg.sections, err = pkg.readSections(pkg.PkgY)

// Load syscfg settings.
pkg.SyscfgY, err = config.ReadFile(pkg.SyscfgYamlPath())
if err != nil && !util.IsNotExist(err) {
Expand Down

0 comments on commit c5566f0

Please sign in to comment.