1

How can one link a variable to a static pre-defined location when using clang/LLVM? For example, some compilers support the following syntax:

static const uint8_t myVar __attribute__ ((at(MY_LOCATION))) = 0x11;

So far I've found that global variables and functions support the section attribute: http://clang.llvm.org/docs/AttributeReference.html#id38 but I'm not sure if that's what I need to use and/or how to make use of it.

The use case for this is that the output of the toolchain is a flat binary that gets loaded into the devices memory. Certain scripts or other tools may expect to find certain attributes of that binary at a fixed offset in the binary image. This is particularly common in the embedded systems world.

EDIT: I also found this https://github.com/llvm-mirror/clang/blob/master/test/Sema/attr-section.c which appears to be a unit testing framework for that attribute. The passing cases have two strings, I don't really understand why there isn't just a single string for a section name.

NickHalden
  • 1,469
  • 2
  • 20
  • 31
  • 2
    Q: Why do you need to assign a variable to a fixed address? Understanding your "use case" would definitely help. – paulsm4 Oct 02 '15 at 18:36
  • Added an explanation of my intended use case. – NickHalden Oct 02 '15 at 18:39
  • Suggestion: take a look at this: [Is there any standard way of embedding resources into an executable image?](http://stackoverflow.com/questions/5479691/is-there-any-standard-way-of-embedding-resources-into-linux-executable-image) – paulsm4 Oct 02 '15 at 18:51
  • 3
    The code base for an embedded system should contain a linker control file. That file contains a memory map that specifies the address and size for all of the standard sections (e.g. text, bss, data). If you create a non-standard section with the section attribute, then you'll need to add the corresponding entry in the linker control file. – user3386109 Oct 02 '15 at 18:53
  • @paulsm4 objcopy works with ELF files and clang produces mach-o files. They are not really compatible. – NickHalden Oct 02 '15 at 18:59
  • @user3386109 Agreed, I guess I'm not really sure what the section attribute I linked to is expecting, why are there two strings separated by a comma? – NickHalden Oct 02 '15 at 19:00

0 Answers0