<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/90358>90358</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Putting a function in a special `__TEXT,__foo` section results in underalignment
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
dougsonos
</td>
</tr>
</table>
<pre>
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 `MachineFunction`s 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.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyEVk9v2z4S_TT0ZWBDpvxPBx8cp1n0ULRAgsXeDEocS9xSpMEZNfF--gUpy1acpL9AUExKfPP43sxQisjUDnErlg9i-ThRHTc-b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