1.10 -fdata-sections, -fno-data-sections

Enables or disables the generation of one ELF section for each variable in the source file. The default is -fdata-sections.

Note:

If you want to place specific data items or structures in separate sections, mark them individually with __attribute__((section("name"))).

Example

volatile int a = 9;
volatile int c = 10;
volatile int d = 11;
				
int main(void){
    static volatile int b = 2;
    return a == b;
}		

Compile this code with:

armclang --target=arm-arm-none-eabi -march=armv8-a -fdata-sections -c -O3 main.c

Use fromelf to see the data sections:

fromelf -cds main.o

...
    Symbol table .symtab (17 symbols, 11 local)

      #  Symbol Name                Value      Bind  Sec  Type  Vis  Size
    ========================================================================
 
     10  .L_MergedGlobals           0x00000000   Lc   10  Data  De   0x8
     11  main.b                     0x00000004   Lc   10  Data  De   0x4
     12  ...
     13  ...
     14  a                          0x00000000   Gb   10  Data  De   0x4
     15  c                          0x00000000   Gb    7  Data  Hi   0x4
     16  d                          0x00000000   Gb    8  Data  Hi   0x4
...			

If you compile this code with -fno-data-sections, you get:

    Symbol table .symtab (15 symbols, 10 local)

      #  Symbol Name                Value      Bind  Sec  Type  Vis  Size
    ========================================================================

      8  .L_MergedGlobals           0x00000008   Lc    7  Data  De   0x8
      9  main.b                     0x0000000c   Lc    7  Data  De   0x4
     10  ... 
     11  ...
     12  a                          0x00000008   Gb    7  Data  De   0x4
     13  c                          0x00000000   Gb    7  Data  Hi   0x4
     14  d                          0x00000004   Gb    7  Data  Hi   0x4      
...

If you compare the two Sec columns, you can see that when -fdata-sections is used, the variables are put into different sections. When -fno-data-sections is used, all the variables are put into the same section.

Non-ConfidentialPDF file icon PDF versionDUI0774J
Copyright © 2014–2017, 2019 Arm Limited or its affiliates. All rights reserved.