forked fromzephyrproject-rtos/arduino-core-zephyr
Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork34
extra/gen_rodata_ld: Add gen_rodata_ld tool.#246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
iabdalkader wants to merge1 commit intoarduino:mainChoose a base branch fromiabdalkader:add_gen_rodata
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
25 changes: 25 additions & 0 deletionsextra/gen-rodata-ld/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| ## Rodata Linker Script Generator | ||
| This tool analyzes ELF files and generates linker script fragments for Zephyr | ||
| two-pass linking, separating read-only data sections based on relocations. | ||
| The tool examines `.rodata` sections in a temporary ELF file and generates a linker | ||
| script that places sections with relocations into `.rodata` (copied to RAM by LLEXT) | ||
| and sections without relocations into `.rodata.noreloc` (kept in flash). This | ||
| optimization significantly reduces RAM usage for LLEXT applications. | ||
| ### Getting the tool | ||
| If you have installed the Arduino IDE and the Arduino core for Zephyr, you can | ||
| find the pre-built files in the `.arduino15/packages/arduino/tools/` folder in | ||
| your home directory. You can directly use the tool from there. | ||
| ### Building manually | ||
| To build the tool, you need to have the Go programming language installed; make | ||
| sure you have the `go` command available in your PATH. Then, use the `go build` | ||
| command to build the tool for your platform. | ||
| To build the full set of binaries for all platforms, run the `package_tool.sh` | ||
| script in the parent directory with `../package_tool.sh`, or provide the path | ||
| to this directory as an argument. |
136 changes: 136 additions & 0 deletionsextra/gen-rodata-ld/main.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package main | ||
| import ( | ||
| "debug/elf" | ||
| "flag" | ||
| "fmt" | ||
| "os" | ||
| "sort" | ||
| "strings" | ||
| ) | ||
| func main() { | ||
| flag.Usage = func() { | ||
| fmt.Fprintf(os.Stderr, "Usage: %s <input.elf> [output_script.ld] [link_mode]\n\n", os.Args[0]) | ||
| fmt.Fprintf(os.Stderr, "Analyzes an ELF file and generates a linker script fragment that\n") | ||
| fmt.Fprintf(os.Stderr, "separates .rodata sections into:\n") | ||
| fmt.Fprintf(os.Stderr, " .rodata - sections WITH relocations (copied to RAM)\n") | ||
| fmt.Fprintf(os.Stderr, " .rodata.noreloc - sections WITHOUT relocations (kept in flash)\n") | ||
| fmt.Fprintf(os.Stderr, "\nIf link_mode is 'static', generates an empty linker script.\n") | ||
| } | ||
| flag.Parse() | ||
| if flag.NArg() < 1 { | ||
| flag.Usage() | ||
| os.Exit(1) | ||
| } | ||
| inputFile := flag.Arg(0) | ||
| outputFile := "rodata_split.ld" | ||
| if flag.NArg() >= 2 { | ||
| outputFile = flag.Arg(1) | ||
| } | ||
| linkMode := "dynamic" | ||
| if flag.NArg() >= 3 { | ||
| linkMode = flag.Arg(2) | ||
| } | ||
| fmt.Printf("Generate rodata linker script (mode: %s)\n", linkMode) | ||
| // For static linking, generate empty linker script | ||
| if linkMode == "static" { | ||
| out, err := os.Create(outputFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer out.Close() | ||
| fmt.Fprintf(out, "/* Empty linker script for static linking mode */\n") | ||
| fmt.Printf("Generated: %s\n", outputFile) | ||
| return | ||
| } | ||
| f, err := elf.Open(inputFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer f.Close() | ||
| // First pass: find all .rodata sections, indexed by section number | ||
| type RodataSection struct { | ||
| Name string | ||
| HasRelocs bool | ||
| } | ||
| rodataSections := make(map[uint32]*RodataSection) | ||
| for i, section := range f.Sections { | ||
| if strings.HasPrefix(section.Name, ".rodata") { | ||
| rodataSections[uint32(i)] = &RodataSection{Name: section.Name} | ||
| } | ||
| } | ||
| // Second pass: mark which rodata sections have relocations | ||
| for _, section := range f.Sections { | ||
| if section.Type == elf.SHT_REL || section.Type == elf.SHT_RELA { | ||
| if rodata, exists := rodataSections[section.Info]; exists { | ||
| rodata.HasRelocs = true | ||
| } | ||
| } | ||
| } | ||
| // Separate and sort | ||
| withRelocs := []string{} | ||
| withoutRelocs := []string{} | ||
| for _, rodata := range rodataSections { | ||
| if rodata.HasRelocs { | ||
| withRelocs = append(withRelocs, rodata.Name) | ||
| } else { | ||
| withoutRelocs = append(withoutRelocs, rodata.Name) | ||
| } | ||
| } | ||
| sort.Strings(withRelocs) | ||
| sort.Strings(withoutRelocs) | ||
| // Generate linker script | ||
| out, err := os.Create(outputFile) | ||
| if err != nil { | ||
| fmt.Fprintf(os.Stderr, "Error: %v\n", err) | ||
| os.Exit(1) | ||
| } | ||
| defer out.Close() | ||
| // Helper function to print a section | ||
| printSection := func(out *os.File, sectionName string, sections []string) { | ||
| fmt.Fprintf(out, " %s : {\n", sectionName) | ||
| for _, name := range sections { | ||
| fmt.Fprintf(out, " *(%s)\n", name) | ||
| } | ||
| fmt.Fprintf(out, " }\n\n") | ||
| } | ||
| fmt.Fprintf(out, "/* Auto-generated linker script fragment for LLEXT\n") | ||
| fmt.Fprintf(out, " * Separates .rodata sections based on relocation status\n") | ||
| fmt.Fprintf(out, " */\n\n") | ||
| fmt.Fprintf(out, "SECTIONS\n{\n") | ||
| fmt.Fprintf(out, " /* Read-only data WITH relocations - will be copied to RAM by LLEXT */\n") | ||
| printSection(out, ".rodata", withRelocs) | ||
| fmt.Fprintf(out, " /* Read-only data WITHOUT relocations - kept in flash by LLEXT */\n") | ||
| printSection(out, ".rodata.noreloc", withoutRelocs) | ||
| fmt.Fprintf(out, " /* Merge all .rel.rodata.* sections into .rel.rodata */\n") | ||
| printSection(out, ".rel.rodata", []string{".rel.rodata", ".rel.rodata.*"}) | ||
| fmt.Fprintf(out, " /* Merge all .rela.rodata.* sections into .rela.rodata */\n") | ||
| printSection(out, ".rela.rodata", []string{".rela.rodata", ".rela.rodata.*"}) | ||
| fmt.Fprintf(out, "}\n") | ||
| fmt.Printf("Generated: %s\n", outputFile) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.