<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/107069>107069</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LLD][RISCV] Why LLD places global_pointer only by looking at sdata_begin
</td>
</tr>
<tr>
<th>Labels</th>
<td>
lld
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
LukeZhuang
</td>
</tr>
</table>
<pre>
In RISCV, lld places `global_pointer` by looking for sdata section, and place it at `sdata_begin + 0x800`, otherwise place it at `elfHeader + 0x800` (the following code is from lld/ELF/Writer.cpp in <4befe65c>):
```
// RISC-V's gp can address +/- 2 KiB, set it to .sdata + 0x800. This symbol
// should only be defined in an executable. If .sdata does not exist, its
// value/section does not matter but it has to be relative, so set its
// st_shndx arbitrarily to 1 (ctx.out.elfHeader).
if (config->emachine == EM_RISCV) {
if (!config->shared) {
OutputSection *sec = findSection(".sdata");
addOptionalRegular("__global_pointer$", sec ? sec : ctx.out.elfHeader,
0x800, STV_DEFAULT);
// Set riscvGlobalPointer to be used by the optional global pointer
// relaxation.
if (config->relaxGP) {
Symbol *s = symtab.find("__global_pointer$");
if (s && s->isDefined())
ctx.sym.riscvGlobalPointer = cast<Defined>(s);
}
}
}
```
The issue is currently we have a program on our target machine that does not have .sdata section at all, thus I found it places global pointer at `0x10000 + 0x800`, which is pretty far away from the symbols we want to relax in .sbss section.
So I just wonder is there a reason that lld decides to place global pointer with this simple strategy? I saw GNU LD uses such way to calculate it in the default linker script:
```
__global_pointer$ = MIN(__SDATA_BEGIN__ + 0x800,
MAX(__DATA_BEGIN__ + 0x800, __BSS_END__ - 0x800));
```
Can LLD have the same behaviour as GNU LD in this case? Thanks!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vl1v4jgU_TXm5apR6gCFBx5oA120TGc0dGZW-xLdxDeJpyZGtsPHv1_ZCaW0nUWoadP7ee65x0ZrZdUQzd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