Skip to content

Commit 2130602

Browse files
Merge pull request #105 from maejima-fumika/bugfix/add-linkerscript-reader
Fix compiler to read ld file of ESP-IDF during linking.
2 parents 4dde9b0 + 965ab35 commit 2130602

File tree

4 files changed

+39
-11
lines changed

4 files changed

+39
-11
lines changed

cli/src/commands/project/run.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ class ESP32RunHandler extends RunHandler {
158158
return {
159159
runtimeDir,
160160
compilerToolchainDir: this.boardConfig.xtensaGccDir,
161+
espDir: this.boardConfig.rootDir
161162
}
162163
}
163164

cli/src/commands/repl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ class ESP32ReplHandler extends ReplHandler {
185185
return {
186186
runtimeDir,
187187
compilerToolchainDir: this.boardConfig.xtensaGccDir,
188+
espDir: this.boardConfig.rootDir
188189
}
189190
}
190191

lang/src/compiler/compiler.ts

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export type PackageConfig = {
2424
export type CompilerConfig = {
2525
runtimeDir: string,
2626
compilerToolchainDir: string,
27+
espDir: string
2728
};
2829

2930
export type MemoryLayout = {
@@ -87,7 +88,7 @@ export class Compiler {
8788
packageReader: (name: string) => PackageConfig,
8889
) {
8990
this.config = config;
90-
this.espidfComponents = new ESPIDFComponents(config.runtimeDir);
91+
this.espidfComponents = new ESPIDFComponents(config.runtimeDir, config.espDir);
9192
this.memory = new ShadowMemory(memoryLayout);
9293
this.packageReader = packageReader;
9394
const elfReader = new ElfReader(RUNTIME_ELF_PATH(this.config));
@@ -177,7 +178,8 @@ export class Compiler {
177178
this.memory,
178179
[...this.definedSymbols.values()],
179180
mainEntryPoint,
180-
subModuleEntryPoints
181+
subModuleEntryPoints,
182+
this.espidfComponents.ldFiles
181183
);
182184
fs.writeFileSync(LINKER_SCRIPT(mainPackage), linkerscript);
183185
await executeCommand(`${LD_PATH(this.config)} -o ${LINKED_ELF_PATH(mainPackage)} -T ${LINKER_SCRIPT(mainPackage)} --gc-sections`, cwd);
@@ -202,7 +204,7 @@ export class Compiler {
202204
const espArchivesFromPkg = this.espidfComponents.getArchiveFilePaths(pkg.espIdfComponents);
203205
espArchivesFromPkg.forEach(ar => addEspArchive(ar));
204206
}
205-
this.espidfComponents.commonArchiveFilePaths.forEach(ar => addEspArchive(ar));
207+
this.espidfComponents.commonArchiveFiles.forEach(ar => addEspArchive(ar));
206208
return resultArchives;
207209
}
208210

@@ -350,7 +352,8 @@ class TranspilerWithPkgSystem {
350352

351353
class ESPIDFComponents {
352354
public readonly commonIncludeDirs: string[];
353-
public readonly commonArchiveFilePaths: string[];
355+
public readonly commonArchiveFiles: string[];
356+
public readonly ldFiles: string[];
354357

355358
private readonly COMPONENTS_PATH_PREFIX = /^.*microcontroller/;
356359
private readonly COMMON_COMPONENTS = ["cxx", "newlib", "freertos", "esp_hw_support", "heap", "log", "soc", "hal", "esp_rom", "esp_common", "esp_system", "xtensa"];
@@ -366,13 +369,14 @@ class ESPIDFComponents {
366369
include_dirs: string[]
367370
}};
368371

369-
constructor(runtimeDir: string) {
372+
constructor(runtimeDir: string, espDir: string) {
370373
this.RUNTIME_DIR = runtimeDir;
371374
this.SDK_CONFIG_DIR = path.join(runtimeDir, 'ports/esp32/build/config');
372375
const dependenciesFile = path.join(runtimeDir, 'ports/esp32/build/project_description.json');
373376
this.dependenciesInfo = JSON.parse(fs.readFileSync(dependenciesFile).toString()).build_component_info;
374377
this.commonIncludeDirs = this.getIncludeDirs(this.COMMON_COMPONENTS);
375-
this.commonArchiveFilePaths = this.getArchiveFilePaths(this.COMMON_COMPONENTS);
378+
this.commonArchiveFiles = this.getArchiveFilePaths(this.COMMON_COMPONENTS);
379+
this.ldFiles = this.getLdFiles(espDir);
376380
}
377381

378382
public getIncludeDirs(rootComponentNames: string[]) {
@@ -387,6 +391,20 @@ class ESPIDFComponents {
387391
return includeDirs;
388392
}
389393

394+
private getLdFiles(espDir: string) {
395+
// These paths are extracted from logs of `idf.py build` command.
396+
// Should be improved.
397+
return [
398+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.ld`),
399+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.api.ld`),
400+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.libgcc.ld`),
401+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.newlib-data.ld`),
402+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.syscalls.ld`),
403+
path.join(espDir, `esp-idf/components/esp_rom/esp32/ld/esp32.rom.newlib-funcs.ld`),
404+
path.join(espDir, `esp-idf/components/soc/esp32/ld/esp32.peripherals.ld`),
405+
]
406+
}
407+
390408
public getArchiveFilePaths(rootComponentNames: string[]) {
391409
return this.getComponents(rootComponentNames).map(c => c.file);
392410
}
@@ -410,10 +428,6 @@ class ESPIDFComponents {
410428
}
411429
return components;
412430
}
413-
414-
private convertRuntimeDirPath(absolutePath: string) {
415-
return absolutePath.replace(this.COMPONENTS_PATH_PREFIX, this.RUNTIME_DIR);
416-
}
417431
}
418432

419433

lang/src/compiler/linker-script.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ export default function generateLinkerScript(
66
shadowMemory: ShadowMemory,
77
definedSymbols: {name: string, address: number}[],
88
entryPoint: string,
9-
subModuleEntryPoints: string[]
9+
subModuleEntryPoints: string[],
10+
includeFiles: string[]
1011
): string {
1112
const iramMemory = new MemoryRegion(
1213
"IRAM",
@@ -52,6 +53,7 @@ export default function generateLinkerScript(
5253

5354
return new LinkerScript()
5455
.group(inputFiles)
56+
.includes(includeFiles)
5557
.entry(entryPoint)
5658
.extern(subModuleEntryPoints)
5759
.memory([iramMemory, dramMemory, iflashMemory, dflashMemory, externalMemory])
@@ -67,6 +69,8 @@ class LinkerScript {
6769
entry(entry: string) { this.commands.push(new Entry(entry)); return this; }
6870
sections(sections: Section[]) { this.commands.push(new Sections(sections)); return this; }
6971
memory(regions: MemoryRegion[]) { this.commands.push(new Memory(regions)); return this; }
72+
include(file: string) { this.commands.push(new Include(file)); return this; }
73+
includes(files: string[]) { files.forEach(file => this.include(file)); return this; }
7074

7175
toString() {
7276
return this.commands.map(c => c.toString()).join('\n');
@@ -85,6 +89,14 @@ class Group implements Command {
8589
}
8690
}
8791

92+
class Include implements Command {
93+
constructor(private file: string) {}
94+
95+
toString(): string {
96+
return `INCLUDE "${this.file}"`;
97+
}
98+
}
99+
88100
class Extern implements Command {
89101
constructor(private symbols: string[]) {}
90102

0 commit comments

Comments
 (0)