0

It seems that adding -Wl,-sectcreate,__RESTRICT,__restrict,/dev/null in Other Linker Flags will add an empty section.

And in code we can __attribute((used,section("segmentname,sectionname"))) to declare a var or function.

But how to declare an empty section in code?

Karl
  • 665
  • 4
  • 19

1 Answers1

1

You already discovered the section(...) attribute, but no matter what you apply it to (even struct {} and char[0]), it's gonna take up some space and create a non-empty segment.

It seems there is no way to do exactly what you're asking from actual C code, but you can achieve a workaround by using inline assembly.

Include this code block somewhere outside of a function:

asm(".section __RESTRICT,__restrict\n"
    "empty:\n"
    ".no_dead_strip empty\n");

This will:

  • add a truly empty (filesize: 0) segment/section to your object file.
  • add an empty symbol to your symbol table, but:
    • it's gonna be private (i.e. non-linkable).
    • as long as it doesn't start with an underscore, the chances of it colliding with anything else are virtually zero.
  • work in all of i386, x86_64, arm and arm64 assembly.
Siguza
  • 21,155
  • 6
  • 52
  • 89