Index: ELF/InputFiles.cpp =================================================================== --- ELF/InputFiles.cpp +++ ELF/InputFiles.cpp @@ -618,6 +618,14 @@ if (Name.startswith(".gnu.linkonce.")) return &InputSection::Discarded; + // If we are creating a new .build-id section, strip existing .build-id + // sections so that the output won't have more than one .build-id. + // This is not usually a problem because input object files normally don't + // have .build-id sections, but you can create such files by + // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it. + if (Name == ".note.gnu.build-id" && Config->BuildId != BuildIdKind::None) + return &InputSection::Discarded; + // The linker merges EH (exception handling) frames and creates a // .eh_frame_hdr section for runtime. So we handle them with a special // class. For relocatable outputs, they are just passed through. Index: test/ELF/relocatable-build-id.s =================================================================== --- test/ELF/relocatable-build-id.s +++ test/ELF/relocatable-build-id.s @@ -0,0 +1,12 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: ld.lld --build-id=0xcafebabe -o %t2.o %t1.o -r +# RUN: ld.lld --build-id=0xdeadbeef -o %t.exe %t2.o +# RUN: llvm-objdump -s %t.exe | FileCheck %s + +# CHECK-NOT: cafebabe +# CHECK: deadbeef + +.global _start +_start: + ret