27

In the following line of code (which declares a global variable),

unsigned int __attribute__((section(".myVarSection,\"aw\",@nobits#"))) myVar;

what does the "aw" flag mean?

My understanding is that the nobits flag will prevent the variable from being initialised to zero, but I am struggling to find info about the "aw" flag.

Also, what meaning do the @ and # have around the nobits flag?

Duncan Drennan
  • 871
  • 2
  • 9
  • 21

2 Answers2

34

The section("section-name") attribute places a variable in a specific section by producing the following assembler line:

.section    section-name,"aw",@progbits

When you set section-name to ".myVarSection,\"aw\",@nobits#" you exploit a kind of "code injection" in GCC to produce:

.section    .myVarSection,"aw",@nobits#,"aw",@progbits

Note that # sign starts a one-line comment.

See GNU Assembler manual for the full description of .section directive. A general syntax is

.section name [, "flags"[, @type[,flag_specific_arguments]]]

so "aw" are flags:

  • a: section is allocatable
  • w: section is writable

and @nobits is a type:

  • @nobits: section does not contain data (i.e., section only occupies space)

All the above is also applicable to functions, not just variables.

Ilia K.
  • 4,822
  • 2
  • 22
  • 19
  • 1
    Note that this code injection doesn’t work in Clang (but honest toplevel inline assembly does, to an extent). – Alex Shpilkin Jun 14 '21 at 18:09
  • Why do I need to specify in source code that the section is executable or writeable? This doesn't make sense to me. A compiler should not need that knowledge as this is the responsibility of the linker and the linker script. However, having those flags seems to be very important, otherwise sections with custom names get discarded. Why is this so? – phip1611 Sep 02 '22 at 22:22
5

what does the "aw" flag mean?

It means that the section is allocatable (i.e. it's loaded to the memory at runtime) and writable (and readable, of course).

My understanding is that the nobits flag will prevent the variable from being initialised to zero, but I am struggling to find info about the "aw" flag.

Also, what meaning do the @ and # have around the nobits flag?

@nobits (@ is just a part of the name) means that the section isn't stored in the image on disk, it only exists in runtime (and it's filled with zeros at the startup).

# character begins the comment, so whatever the compiler will put in addition to what you have specified will be ignored.

Community
  • 1
  • 1
  • So how do I stop the variable from being initialised to zero at startup? – Duncan Drennan Jun 06 '11 at 21:12
  • If you use @progbits instead of @nobits, it won't be initialized to zero at startup, but it'll be stored in the image on disk instead (and it'll still be zero if you don't specify another value). What are you trying to achieve? –  Jun 07 '11 at 08:25
  • I wanted to pass a flag (in RAM) between a bootloader and an application on an embedded microcontroller. I figured out that adding a section in the linker file prevented the variable from being initialised. – Duncan Drennan Jun 08 '11 at 07:21
  • Where can I find the preprocessed file? I wish to know what is being commented out. – David Feb 19 '18 at 08:50