diff --git a/llvm/lib/MC/MCMachOStreamer.cpp b/llvm/lib/MC/MCMachOStreamer.cpp --- a/llvm/lib/MC/MCMachOStreamer.cpp +++ b/llvm/lib/MC/MCMachOStreamer.cpp @@ -31,6 +31,7 @@ #include "llvm/MC/TargetRegistry.h" #include "llvm/Support/Casting.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/LEB128.h" #include "llvm/Support/raw_ostream.h" #include #include @@ -593,5 +594,9 @@ MCSection *AddrSigSection = Asm.getContext().getObjectFileInfo()->getAddrSigSection(); Asm.registerSection(*AddrSigSection); - new MCDataFragment(AddrSigSection); + auto *Frag = new MCDataFragment(AddrSigSection); + size_t SectionBytes = 0; + for (const MCSymbol *sym : writer.getAddrsigSyms()) + SectionBytes += getULEB128Size(sym->getIndex()); + Frag->getContents().resize(SectionBytes); } diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp --- a/llvm/lib/MC/MachObjectWriter.cpp +++ b/llvm/lib/MC/MachObjectWriter.cpp @@ -766,6 +766,7 @@ MCDataFragment *pDataFragment = dyn_cast_or_null(pFragment); assert(pDataFragment); + pDataFragment->getContents().clear(); raw_svector_ostream OS(pDataFragment->getContents()); for (const MCSymbol *sym : this->getAddrsigSyms()) encodeULEB128(sym->getIndex(), OS); diff --git a/llvm/test/CodeGen/AArch64/addrsig-macho.ll b/llvm/test/CodeGen/AArch64/addrsig-macho.ll --- a/llvm/test/CodeGen/AArch64/addrsig-macho.ll +++ b/llvm/test/CodeGen/AArch64/addrsig-macho.ll @@ -1,7 +1,16 @@ ; RUN: llc -filetype=asm %s -o - -mtriple arm64-apple-ios -addrsig | FileCheck %s ; RUN: llc -filetype=obj %s -o %t -mtriple arm64-apple-ios -addrsig +; RUN: llvm-objdump --macho --section-headers %t | FileCheck %s --check-prefix=SECTIONS ; RUN: llvm-readobj --hex-dump='__llvm_addrsig' %t | FileCheck %s --check-prefix=OBJ +; The __debug_line section (which should be generated for the given input file) +; should appear immediately after the addrsig section. Use it to make sure +; addrsig's section size has been properly set during section layout. This +; catches a regression where the next section would overlap addrsig's +; contents. +; SECTIONS: __llvm_addrsig [[#%.8x,SIZE:]] [[#%.16x,ADDR:]] +; SECTIONS-NEXT: __debug_line {{[[:xdigit:]]+}} [[#%.16x, SIZE+ADDR]] + ; CHECK: .addrsig{{$}} ; CHECK-NEXT: .addrsig_sym _func03_takeaddr ; CHECK-NEXT: .addrsig_sym _f1 @@ -38,7 +47,7 @@ } ; Function Attrs: minsize nofree noinline norecurse nounwind optsize ssp uwtable -define void @func03_takeaddr() #0 { +define void @func03_takeaddr() #0 !dbg !9 { entry: %0 = load volatile i32, ptr @result, align 4 %add = add nsw i32 %0, 1 @@ -72,8 +81,8 @@ %a2 = bitcast i32* @a2 to i8* %i1 = bitcast void()* @i1 to i8* %i2 = bitcast void()* @i2 to i8* - call void @llvm.dbg.value(metadata i8* bitcast (void()* @metadata_f1 to i8*), metadata !5, metadata !DIExpression()), !dbg !7 - call void @llvm.dbg.value(metadata i8* bitcast (void()* @metadata_f2 to i8*), metadata !5, metadata !DIExpression()), !dbg !7 + call void @llvm.dbg.value(metadata i8* bitcast (void()* @metadata_f1 to i8*), metadata !6, metadata !DIExpression()), !dbg !8 + call void @llvm.dbg.value(metadata i8* bitcast (void()* @metadata_f2 to i8*), metadata !6, metadata !DIExpression()), !dbg !8 call void @f4(i8* bitcast (void()* @metadata_f2 to i8*)) unreachable } @@ -108,9 +117,18 @@ declare void @llvm.dbg.value(metadata, metadata, metadata) attributes #0 = { noinline } - -!3 = distinct !DISubprogram(scope: null, isLocal: false, isDefinition: true, isOptimized: false) -!4 = !DILocation(line: 0, scope: !3) -!5 = !DILocalVariable(scope: !6) -!6 = distinct !DISubprogram(scope: null, isLocal: false, isDefinition: true, isOptimized: false) -!7 = !DILocation(line: 0, scope: !6, inlinedAt: !4) +!llvm.dbg.cu = !{!0} +!llvm.module.flags = !{!2, !3} + +!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, emissionKind: FullDebug) +!1 = !DIFile(filename: "test.c", directory: "/tmp") +!2 = !{i32 7, !"Dwarf Version", i32 4} +!3 = !{i32 2, !"Debug Info Version", i32 3} + +!4 = distinct !DISubprogram(scope: null, isLocal: false, isDefinition: true, isOptimized: false, unit: !0) +!5 = !DILocation(line: 0, scope: !4) +!6 = !DILocalVariable(scope: !7) +!7 = distinct !DISubprogram(scope: null, isLocal: false, isDefinition: true, isOptimized: false, unit: !0) +!8 = !DILocation(line: 0, scope: !7, inlinedAt: !5) +!9 = distinct !DISubprogram(scope: null, file: !1, line: 1, type: !10, unit: !0) +!10 = !DISubroutineType(types: !{})