Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -1342,9 +1342,18 @@ // Apply symbol renames for -wrap. Symtab->applySymbolWrap(); - // Now that we have a complete list of input files. - // Beyond this point, no new files are added. - // Aggregate all input sections into one place. + // Now that we have a complete list of input files. Beyond this point, no new + // files are added. Aggregate all input sections into one place. + // + // At this point, the list might be not empty and contain synthetic sections + // created for common symbols. We want to move them to the end of input + // sections list. That way they will be grouped with other synthetics we will + // add. This helps sometimes to resolve relocations properly. For example, + // gcc/x86_64-linux-gnu/5.4.0/crtbegin.o has 32bit relocation against it's + // own .bss. If user's code has huge common symbol, it would be placed at the + // start of ".bss" and may cause overflow. + std::vector Commons; + Commons.swap(InputSections); for (InputFile *F : ObjectFiles) for (InputSectionBase *S : F->getSections()) if (S && S != &InputSection::Discarded) @@ -1352,6 +1361,8 @@ for (BinaryFile *F : BinaryFiles) for (InputSectionBase *S : F->getSections()) InputSections.push_back(cast(S)); + for (InputSectionBase *S : Commons) + InputSections.push_back(S); // We do not want to emit debug sections if --strip-all // or -strip-debug are given. Index: test/ELF/bss-reloc-overflow.s =================================================================== --- test/ELF/bss-reloc-overflow.s +++ test/ELF/bss-reloc-overflow.s @@ -0,0 +1,15 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t +# RUN: ld.lld %t -o %t2 + +## Previously we were not able to link this code. +## It has R_X86_64_PC32 relocation which overflowed. + +.text +.long .bss - . + +.bss +.byte 0x0 + +.type arr,@object +.comm arr,0xffffffff,4