Skip to content

Clang: Putting a function in a special __TEXT,__foo section results in underalignment #90358

Closed
@dougsonos

Description

@dougsonos

To reproduce (on macOS):

// section_test.cpp
struct Class {
	__attribute__((section("__TEXT,__foo,regular,pure_instructions")))
	static int test(int x, int y)
	{
		return x + y;
	}
};

int main(int argc, const char*[])
{
	return Class::test(argc, argc);
}

// clang -c -o section_test.o section_test.cpp
// otool -l section_test.o

Output is:

Section
  sectname __foo
   segname __TEXT
      addr 0x0000000000000030
      size 0x0000000000000020
    offset 520
     align 2^1 (2)   <<<<<<<<<<< underaligned section

I would have hoped for an alignment of 4 because that is the (sub)target's getMinFunctionAlignment().

Analysis so far:

  • Section alignment comes from the most-aligned object in it - MCObjectStreamer
  • the llvm::Function has an alignment of 2 (comments say: for C++ ABI reasons)
  • the MachineFunction has an alignment of 4 (for arm64)
  • normally the MachineFunction's alignment is used, but because hasSection() is true for the Function, the alignment of 2 wins (logic in AsmPrinter::getGVAlignment)
  • the MCSection remains aligned to 2 because all of its functions are aligned to 2
  • the normal text section's alignment is 4 because the MachineFunctions are 4-byte-aligned and not overridden by AsmPrinter

This seems like a catch-22: the MachineFunction alignment should win, but is getting overridden by the section's alignment, yet the section's alignment comes from the llvm::Function, which doesn't know anything at all (would use 1 except for a C++ ABI reason making it 2).

Interestingly, a function that is not a C++ method declared inline in its class, if it has a section attribute, is aligned correctly.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions