Index: lld/ELF/InputFiles.cpp =================================================================== --- lld/ELF/InputFiles.cpp +++ lld/ELF/InputFiles.cpp @@ -618,6 +618,13 @@ 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. + // Usually, input object files don't contain .build-id sections, but if you + // create them with "ld -r --build-id", they contain 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: lld/test/ELF/relocatable-build-id.s =================================================================== --- /dev/null +++ lld/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 +# 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