diff --git a/llvm/test/tools/llvm-objcopy/MachO/add-multiple-sections.test b/llvm/test/tools/llvm-objcopy/MachO/add-multiple-sections.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-objcopy/MachO/add-multiple-sections.test @@ -0,0 +1,121 @@ +## This test verifies that llvm-objcopy can add multiple sections to a Mach-O binary. + +# RUN: yaml2obj %s -o %t +# RUN: echo -n FOOabcdefg > %t.foo.data +# RUN: echo -n BARabcdefg > %t.bar.data + +## Case 1: Add a new sections twice into an existing segment. +# RUN: llvm-objcopy --add-section __TEXT,__foo=%t.foo.data %t %t.foo.out +# RUN: llvm-objcopy --add-section __TEXT,__bar=%t.bar.data %t.foo.out %t.foo.bar.out +# RUN: llvm-readobj --sections --section-data %t.foo.bar.out \ +# RUN: | FileCheck %s + +## Case 2: Add a two new sections into an existing segment. +# RUN: llvm-objcopy --add-section __TEXT,__foo=%t.foo.data --add-section __TEXT,__bar=%t.bar.data %t %t.foo_bar.out +# RUN: llvm-readobj --sections --section-data %t.foo_bar.out \ +# RUN: | FileCheck %s + +--- !mach-o +FileHeader: + magic: 0xFEEDFACF + cputype: 0x01000007 + cpusubtype: 0x00000003 + filetype: 0x00000001 + ncmds: 1 + sizeofcmds: 152 + flags: 0x00002000 + reserved: 0x00000000 +LoadCommands: + - cmd: LC_SEGMENT_64 + cmdsize: 152 + segname: __TEXT + vmaddr: 0 + vmsize: 4 + fileoff: 184 + filesize: 4 + maxprot: 7 + initprot: 7 + nsects: 1 + flags: 0 + Sections: + - sectname: __text + segname: __TEXT + addr: 0x0000000000000000 + content: 'AABBCCDD' + size: 4 + offset: 184 + align: 0 + reloff: 0x00000000 + nreloc: 0 + flags: 0x80000400 + reserved1: 0x00000000 + reserved2: 0x00000000 + reserved3: 0x00000000 + +# CHECK: Format: Mach-O 64-bit x86-64 +# CHECK: Arch: x86_64 +# CHECK: AddressSize: 64bit +# CHECK: Sections [ +# CHECK: Section { +# CHECK: Index: 0 +# CHECK: Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00) +# CHECK: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00) +# CHECK: Address: 0x0 +# CHECK: Size: 0x4 +# CHECK: Offset: 344 +# CHECK: Alignment: 0 +# CHECK: RelocationOffset: 0x0 +# CHECK: RelocationCount: 0 +# CHECK: Type: Regular (0x0) +# CHECK: Attributes [ (0x800004) +# CHECK: PureInstructions (0x800000) +# CHECK: SomeInstructions (0x4) +# CHECK: ] +# CHECK: Reserved1: 0x0 +# CHECK: Reserved2: 0x0 +# CHECK: Reserved3: 0x0 +# CHECK: SectionData ( +# CHECK: 0000: AABBCCDD |....| +# CHECK: ) +# CHECK: } +# CHECK: Section { +# CHECK: Index: 1 +# CHECK: Name: __foo (5F 5F 66 6F 6F 00 00 00 00 00 00 00 00 00 00 00) +# CHECK: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00) +# CHECK: Address: 0x4 +# CHECK: Size: 0xA +# CHECK: Offset: 348 +# CHECK: Alignment: 0 +# CHECK: RelocationOffset: 0x0 +# CHECK: RelocationCount: 0 +# CHECK: Type: Regular (0x0) +# CHECK: Attributes [ (0x0) +# CHECK: ] +# CHECK: Reserved1: 0x0 +# CHECK: Reserved2: 0x0 +# CHECK: Reserved3: 0x0 +# CHECK: SectionData ( +# CHECK: 0000: 464F4F61 62636465 6667 |FOOabcdefg| +# CHECK: ) +# CHECK: } +# CHECK: Section { +# CHECK: Index: 2 +# CHECK: Name: __bar (5F 5F 62 61 72 00 00 00 00 00 00 00 00 00 00 00) +# CHECK: Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00) +# CHECK: Address: 0xE +# CHECK: Size: 0xA +# CHECK: Offset: 358 +# CHECK: Alignment: 0 +# CHECK: RelocationOffset: 0x0 +# CHECK: RelocationCount: 0 +# CHECK: Type: Regular (0x0) +# CHECK: Attributes [ (0x0) +# CHECK: ] +# CHECK: Reserved1: 0x0 +# CHECK: Reserved2: 0x0 +# CHECK: Reserved3: 0x0 +# CHECK: SectionData ( +# CHECK: 0000: 42415261 62636465 6667 |BARabcdefg| +# CHECK: ) +# CHECK: } +# CHECK: ] diff --git a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp --- a/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp +++ b/llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp @@ -280,6 +280,7 @@ StringRef TargetSegName = Pair.first; Section Sec(TargetSegName, Pair.second); Sec.Content = Obj.NewSectionsContents.save(Buf->getBuffer()); + Sec.Size = Sec.Content.size(); // Add the a section into an existing segment. for (LoadCommand &LC : Obj.LoadCommands) {