Skip to content

Instantly share code, notes, and snippets.

@Diltsman
Created December 15, 2018 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Diltsman/0c9a7c8e2aff06d6e3e5d2d8f9264c3f to your computer and use it in GitHub Desktop.
Save Diltsman/0c9a7c8e2aff06d6e3e5d2d8f9264c3f to your computer and use it in GitHub Desktop.
Issues with linker variable values in Zig
test.zig
// .data related linker variables
extern const _data_begin: u32;
extern const _data_end: u32;
extern var _d: u32;
// .bss related linker variables
extern var _bss_begin: u32;
extern const _bss_end: u32;
extern var _b: u32;
var bss_var: u16 = 0;
var data_var: u16 = 1234;
const rodata_var: u16 = 4567;
nakedcc fn _start() void {
var db = &_data_begin;
var de = &_data_end;
var d_size = @ptrToInt(de) - @ptrToInt(db);
var bb = &_bss_begin;
var be = &_bss_end;
var b_size = @ptrToInt(be) - @ptrToInt(bb);
bss_var = 1;
data_var = 2;
var x = rodata_var;
build.zig
const builtin = @import("builtin");
const Builder = @import("std").build.Builder;
pub fn build(b: *Builder) void {
const mode = b.standardReleaseOptions();
const exe = b.addExecutable("SAM3X8E", "test.zig");
exe.setTarget(builtin.Arch.armv7m, builtin.Os.freestanding, builtin.Environ.eabi);
exe.setBuildMode(mode);
exe.setLinkerScriptPath("build.ld");
const run_step = b.step("run", "Run the app");
const run_cmd = b.addCommand(".", b.env_map, [][]const u8{exe.getOutputPath()});
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(&exe.step);
b.default_step.dependOn(&exe.step);
b.installArtifact(exe);
}
build.ld
MEMORY
{
sram0 (!rx) : ORIGIN = 0x20000000, LENGTH = 64K
sram1 (!rx) : ORIGIN = 0x20080000, LENGTH = 32K
nfcsram (!rx) : ORIGIN = 0x20100000, LENGTH = 4224
flash0 (rx) : ORIGIN = 0x00080000, LENGTH = 256K
flash1 (rx) : ORIGIN = 0x000c0000, LENGTH = 256K
pioa (rw) : ORIGIN = 0x400e0e00, LENGTH = 328
piob (rw) : ORIGIN = 0x400e1000, LENGTH = 328
pioc (rw) : ORIGIN = 0x400e1200, LENGTH = 328
piod (rw) : ORIGIN = 0x400e1400, LENGTH = 328
}
SECTIONS
{
.text :
{
KEEP(*(.vector));
. = ALIGN(4);
*(.text);
}>flash0
. = ALIGN(4);
_d = .;
.data : AT(_d)
{
. = ALIGN(4);
_data_begin = .;
*(.data);
. = ALIGN(4);
_data_end = .;
}>sram0
_b = .;
.bss (NOLOAD) :
{
. = ALIGN(4);
_bss_begin = .;
*(.bss);
. = ALIGN(4);
_bss_end = .;
}>sram0
.rodata :
{
. = ALIGN(4);
*(.rodata);
}>flash0
.data.rel.ro :
{
. = ALIGN(4);
*(.data.rel.ro);
}>flash0
.got :
{
. = ALIGN(4);
*(.got);
}>flash0
.ARM :
{
. = ALIGN(4);
*(.ARM.exidx);
}>flash0
.pioa (NOLOAD) :
{
*(.pioa);
}>pioa
.piob (NOLOAD) :
{
*(.piob);
}>piob
.pioc (NOLOAD) :
{
*(.pioc);
}>pioc
.piod (NOLOAD) :
{
*(.piod);
}>piod
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment