53 lines · plain
1Large data sections2===================3 4When linking very large binaries, lld may report relocation overflows like5 6::7 8 relocation R_X86_64_PC32 out of range: 2158227201 is not in [-2147483648, 2147483647]9 10This happens when running into architectural limitations. For example, in x86-6411PIC code, a reference to a static global variable is typically done with a12``R_X86_64_PC32`` relocation, which is a 32-bit signed offset from the PC. That13means if the global variable is laid out further than 2GB (2^31 bytes) from the14instruction referencing it, we run into a relocation overflow.15 16lld normally lays out sections as follows:17 18.. image:: section_layout.png19 20The largest relocation pressure is usually from ``.text`` to the beginning of21``.rodata`` or ``.text`` to the end of ``.bss``.22 23Some code models offer a tradeoff between relocation pressure and performance.24For example, x86-64's medium code model splits global variables into small and25large globals depending on if their size is over a certain threshold. Large26globals are placed further away from text and we use 64-bit references to refer27to them.28 29Large globals are placed in separate sections from small globals, and those30sections have a "large" section flag, e.g. ``SHF_X86_64_LARGE`` for x86-64. The31linker places large sections on the outer edges of the binary, making sure they32do not affect the distance of small globals to text. The large versions33of ``.rodata``, ``.bss``, and ``.data`` are ``.lrodata``, ``.lbss``, and34``.ldata``, and they are laid out as follows:35 36.. image:: large_section_layout_pic.png37 38We try to keep the number of ``PT_LOAD`` segments to a minimum, so we place39large sections next to the small sections with the same RWX permissions when40possible.41 42``.lbss`` is right after ``.bss`` so that they are merged together and we43minimize the number of segments with ``p_memsz > p_filesz``.44 45Note that the above applies to PIC code. For less common non-PIC code with46absolute relocations instead of relative relocations, 32-bit relocations47typically assume that symbols are in the lower 2GB of the address space. So for48non-PIC code, large sections should be placed after all small sections to avoid49``.lrodata`` pushing small symbols out of the lower 2GB of the address space.50``-z lrodata-after-bss`` changes the layout to be:51 52.. image:: large_section_layout_nopic.png53