Search code examples
linkergnuldlinker-scripts

GNU linker LENGTH section multiplication factor


The length section of the memory command in a linker file takes K, M symbols to indicate kilobytes, megabytes etc. Is the multiplier 1000 or 1024?


Solution

  • I know this is months later but I was wondering about the K and M parts myself.

    The LD manual states that the expression "must be numerical only and evaluate to a constant". It then blithely proceeds to use an example of 256K and 4M which is slightly disconcerting!

    The relevant source code files (in binutils/ld) appear to be ldlex.l and ldgram.y. In the lexer, an expression can end with [M|K|m|k].

    For M or m, the value is multiplied by 1024 * 1024.
    For K or k, the value is multiplied by 1024.

    Edit: I had read sections of the LD manual in isolation, which can be misleading! When I read more of the linker manual, I can see that it does specify what it means by numerical constant, which does include suffixes like K and M: https://sourceware.org/binutils/docs/ld/Constants.html#Constants