4.11 Merging identical constants

The linker can attempt to merge identical constants in objects targeted at AArch32 state. The objects must be produced with Arm® Compiler 6. If you compile with the armclang -ffunction-sections option, the merge is more efficient. This option is the default.

The following procedure is an example that shows the merging feature.

Procedure

  1. Create a C source file, litpool.c, containing the following code:
    int f1() {
        return 0xdeadbeef;
    }
    int f2() {
        return 0xdeadbeef;
    }
  2. Compile the source with -S to create an assembly file:
    armclang -c -S -target arm-arm-none-eabi -mcpu=cortex-m0 -ffunction-sections litpool.c -o litpool.s

    Note:

    -ffunction-sections is the default.
    Results:

    Because 0xdeadbeef is a difficult constant to create using instructions, a literal pool is created, for example:

    ...
    f1:
    	.fnstart
    @ BB#0:
    	ldr	r0, __arm_cp.0_0
    	bx	lr
    	.p2align	2
    @ BB#1:
    __arm_cp.0_0:
    	.long	3735928559              @ 0xdeadbeef
    ...
    	.fnend
    
    ...
    	.code	16                      @ @f2
    	.thumb_func
    f2:
    	.fnstart
    @ BB#0:
    	ldr	r0, __arm_cp.1_0
    	bx	lr
    	.p2align	2
    @ BB#1:
    __arm_cp.1_0:
    	.long	3735928559              @ 0xdeadbeef
    ...
    	.fnend
    ...

    Note:

    There is one copy of the constant for each function, because armclang cannot share these constants between both functions.
  3. Compile the source to create an object:
    armclang -c -target arm-arm-none-eabi -mcpu=cortex-m0 litpool.c -o litpool.o
  4. Link the object file using the --merge_litpools option:
    armlink --cpu=Cortex-M0 --merge_litpools litpool.o -o litpool.axf

    Note:

    --merge_litpools is the default.
  5. Run fromelf to view the image structure:
    fromelf -c -d -s -t -v -z litpool.axf
    Results: The following example shows the result of the merge:
    ...
        f1
            0x00008000:    4801        .H      LDR      r0,[pc,#4] ; [0x8008] = 0xdeadbeef
            0x00008002:    4770        pG      BX       lr
        f2
            0x00008004:    4800        .H      LDR      r0,[pc,#0] ; [0x8008] = 0xdeadbeef
            0x00008006:    4770        pG      BX       lr
        $d.4
        __arm_cp.1_0
            0x00008008:    deadbeef    ....    DCD    3735928559
    ...
Non-ConfidentialPDF file icon PDF versionDUI0803J
Copyright © 2014–2017, 2019 Arm Limited or its affiliates. All rights reserved.