<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/57274>57274</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            References to static data created in .text section are left unresolved
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dr-m
      </td>
    </tr>
</table>

<pre>
    I am working on a LLVM front-end that aims to generate position-independent code that does not require any linking. Basically, each generated module would consist of one function that only interacts with the environment via parameters to the function, like this:
```c
__attribute__((section(".text")))
static const char msg[2][4] = {{ 'o','n','e','\0'}, {'t','w','o','\0'}};

int f (int (*c)(const char*), int d)
{
  return (*c)(msg[d]);
}
```
When compiled as follows, I will get nice self-contained code for the above:
```sh
clang-13 -O2 -fPIC -c f.c
objdump -d f.o

0000000000000000 <f>:
   0:   48 89 f8                mov    %rdi,%rax
   3:   48 63 ce                movslq %esi,%rcx
   6:   48 8d 15 06 00 00 00    lea    0x6(%rip),%rdx        # 13 <msg>
   d:   48 8d 3c 8a             lea    (%rdx,%rcx,4),%rdi
  11:   ff e0                   jmp    *%rax

0000000000000013 <msg>:
  13:   6f 6e 65 00 74 77 6f 00                             one.two.
```
However, when it is compiled with `clang-14` or `clang-15`, the reference will remain unresolved, and the object file would contain two `.text` sections according to `readelf -a`. The reference would be resolved by `lld -shared -o f.so f.o`, but I would not want to invoke any linker.
```
0000000000000000 <f>:
   0:   48 89 f8                mov    %rdi,%rax
   3:   48 63 ce                movslq %esi,%rcx
   6:   48 8d 15 00 00 00 00    lea    0x0(%rip),%rdx        # d <f+0xd>
   d:   48 8d 3c 8a             lea    (%rdx,%rcx,4),%rdi
  11:   ff e0                   jmp    *%rax
```
The following change to LLVM 15 would fix this for me:
```diff
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 941e6a2c648e..636492f757b7 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -474,6 +474,9 @@ static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K) {
       Name.startswith(".llvm.linkonce.tb."))
     return SectionKind::getThreadBSS();
 
+  if (Name == ".text")
+    return SectionKind::getText();
+
   return K;
 }
 
```
I originally found this while developing a program that generates LLVM IR via the C++ interface. It would work in LLVM 9 and 13, but not 14 or 15.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzVV0mv2zYQ_jXyZSBBmyXr4MOzE7dGXhfkBe3xgRIpmwklOiS9vH_fGUreEqMFmksr0BJFcRbO8s241vxtvgbWwVGbL7LfgO6BwfPzH79Aa3TvQtFzcFvmgMnOgtOwEb0wzAnYaSud1H0oey52uE_0DhrNxbCfa2Gh1w6M-LqXRgDr30DJnqREsGBWNkyptyBdgmDN9sKXQ6f5XgnUaK84MuyttA50i6oJaPd9Q0IHGbpXbyB7h4SNs3CUbosfBIj-IFH7jjQ6SAY7ZlgncJs_Ae048yHxSn4hnaUNsqcgfhfET0ERD6MZ3l9fmXNG1nsnXl-DdIbDipEeX9LIiZPDZ5BW4_Bk1jEnG38EtMyWGejsJpgu0mD6Dh85PiDI8FcucECQlhp_qBLe-8tMXGbBdBnTo3xHahNVWrrL1-Nlph9R4MgW4_H8He0GLQqd0cSf46nx2s-uCuOaX1qSlYFfTkay_QTQvW5v-nsGwzE5HRPfL2JRhXvzDq9_bkWPRup2UqH3mYVWK6WPlsSu0alKYXA46GUjwArVhqifY7IXfAi3VhvvU1brg_jeh3Y7LDSK9ZswySD8LYWw_X29hLCBNhp9rOvPfN_tIOS4pkce79NgFgdPxfAaf3Oh85ZtkL2_yASA2L9U-QxmFbQzuL1wvdMHP0unhkvvo6lhpwt1dqEuMsDjfk9t1VeiFvZM3Vypi6tsDskU4gJQyWHguhKM-MSnwj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