Can i generate a traditional "linker map" when building an executable

using just zig build-exe for now to keep things simple, is there a way to get a linker map file – something usually done with a -Wl, command-line option…

why??? i’m targeting bare-metal silicon in which i pass my own linker cmd file via the --script option, and have usually relied on the linker map file as a way to validate things are laid out in memory as prescribed…

these sorts of linker cmd file will frequently define “link time absolute symbols” that are ultimately bound into the final executable – what would sometimes appear as an A symbol when running nm

with zig-build-exe, the final binary has most of that information stripped away…

i have found some github posts that suggest a -map option is a “maybe” feature – but it is absolutely critical for folks like me who target deeply embedded MCUs…

since the build flow for embedded targets invariably have “extra steps” not needed natively (eg., use objcopy to create intel hex files), i suppose i could always use zig-obj to take me most of the way and then use an explicit invocation of some linker once i have a prog.o that effectively is my entire program modulo link-time binding…

thoughts on how might approach this, given the quirks of deeply embedded programming???

No, there is no way to get a map file without invoking the linker.

Note that there is a zig objcopy command and an undocumented zig ld.lld command that accepts the flags: --cref, --Map=, --print-map and -M.
Call them with --help to get their usage and flags. (e.g. zig --help and zig objcopy --help)

But the map information exists in the elf file.
You can use nm, readelf and objdump from binutils, to get a human readable listing.
objdump -h -t -s -d elf-file
-h prints headers for sections such as .text, .data, .bss
-t prints all the symbols with addresses
-s prints all sections as hex dump with ascii (useful for .rodata)
-d disassembles code

nm -S -l elf-file prints addresses and symbols
-S adds the size
-l adds filename and line number

readelf -SsW elf-file
-S prints sections (.rodata, .data, etc)
-s symbol table
-W wide format for symbols (does not limit the symbol name size)

1 Like

i’m already taking the objdump route to harvest information…

for whatever reason, i have “absolute symbols” defined in my linker script that are dropped from the final executable…

but zig ld.lld is a real find… what other undocumented sub-commands are out there ??? :wink:

I am guessing that the linker erases them because they are unused.
In your main reference the symbols like: _ = _flash_start;