<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/66836>66836</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[LLD] LLD can report "unable to move location counter backward" error too early
</td>
</tr>
<tr>
<th>Labels</th>
<td>
lld
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
PiJoules
</td>
</tr>
</table>
<pre>
I have the following linker script:
```
MEMORY
{
...
RAM_DATA (RW) : ORIGIN = 0x20008000, LENGTH = 0x004F8000
...
}
ram_data_end = ORIGIN(RAM_DATA) + LENGTH(RAM_DATA);
SECTIONS {
... // other output sections like text, rodata, etc
__etext = .;
__DATA_ROM = .;
__end_of_code_in_ram_data =
ABSOLUTE(__etext - ORIGIN(RAM_CODE) + ORIGIN(RAM_DATA));
.data __end_of_code_in_ram_data : AT(__DATA_ROM) ALIGN(4) {
__data_start__ = .;
*(.data);
*(.data*);
... // stuff placed here
__data_end__ = .; /* define a global symbol at data end */
} > RAM_DATA
.shared_ram.unused_space (NOLOAD) : ALIGN(4) {
. = ABSOLUTE(ram_data_end);
} >RAM_DATA
... // other output sections
}
```
which would often report this error: `unable to move location counter backward for: .shared_ram.unused_space`. This is because on the first call to `script->assignAddresses();` in `finalizeAddressDependentContent`, all of the input data sections placed into `.data` exceeds the remaining space for RAM_DATA, so by the time we reach `. = ABSOLUTE(ram_data_end);`, DOT is well past the end of RAM_DATA.
However, on subsequent calls to `script->assignAddresses()` in the fixed-point loop in `finalizeAddressDependentContent`, the start of `.data` is changed and a smaller value than the first iteration because something was changed in previous sections. This could be from linker relaxations, sections being removed, allocation sizes changing, etc. Because the starting address of `.data` is smaller but all the input data sections remain the same, it's possible for subsequent runs of `script->assignAddresses()` to not throw the same error because it happens to fit.
The issue here is that lld will still throw this error from the first run even though all the sections can fit as expected in the final memory region after the fixed point loop has finished. I think the right approach here is that LLD should only return this error if DOT ends up moving backwards for the final call to this function, similar to how we delay checking of memory region sizes until the very end of linking.
TODO: Come up with a public-facing minimal reproducer. I haven't made one yet because it's too hard.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVt1u2zgTfRr6ZhBBkfwTX_jCieM2H5z4Q-rFYq8MShxZ3FCklqTsuE-_GEryT9sUWSNwlFCcM-fMmSG5c3KnEWdsdM9GiwFvfGns7P_yf6ZR6AaZEcfZE5R8j-BLhMIoZQ5S70BJ_YYWXG5l7Vk6Z_GCxf33OO5-wp_Pj8_r17-6pcl9-wAQRVH3-Dp_3i7mmzlcf1hy9_onS6YALJ3D-vXpy9MLsHQB8XsSx_FdHMcseYDV48uXzdduIY6Hy7DQRj5hsMniMkPLq63gnm9Riw4sXXQQBNslROAsue8grhdYen8Z8dvjw-Zp_fINrhkSiyVLlmB8iRZM4-vGg8PcS6MdKPmG4PHdExFrKCV6Qp_3MbZbpPWQX3TCpP9THtvX9fOPS1sitTXFNjcCt1Jve7L0Yr-dPvP7b-vVH5tHltz1KDfXGjysF4-9Br8S51IGgCiA_A5-DvNNQOtzp-Dz1dMXijsMSGf1iGOokfPc-u32JwlI2zlL7qJWtunHS1f1uiqL801RQK14jgJKtPgTOrE5Y5-9uWTJHAQWUiNw2CmTcQXuWGVGAfcQGJO7AvqyD8smC2Dp48nylxaCyJXcoiDFokY3DsXW1TxH6oSX9Wo9XwSJSMYPNYtCqhelvbT6tUhdLr9MJYj0O-v-2FbXLd9-H0qZl3AwjRJgCo8aLNbGevCldIDWGktk2DhuNM8UgjdQmT2CMjknFMhNoz1ayHj-duBWQNFu-UgpNo4j2FB06SDDnDcOweh2dknrPORcKcJh47idXTcsfWzH4FwIi86hY8ldp9Q4Bqnp3UJqruR37N5ZYI1aoPYPRnvUnognD0CxTRHQpCa1ggtO3d7ZTOoWvzXnOAZ8zxGFC_ssVlxqmrBt6Qtjz2ZJHsAZyI7hTS8rhAPt4HkZ4n2i9G2ei_WGBDqgUlBz50M8MqspTmDRZSW_mgPu0dJe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