1## Show that if --only-section is given, llvm-objcopy removes all sections
2## except ones specified in the option.
3
4# RUN: yaml2obj %s -o %t
5
6## Specify all sections. The output file should be the same as the input.
7# RUN: llvm-objcopy -j __TEXT,__text -j __DATA,__data -j __TEXT,__const %t %t2
8# RUN: cmp %t %t2
9
10## Specify one section. The output file should contain only that section.
11# RUN: llvm-objcopy --only-section __TEXT,__text %t %t3
12# RUN: llvm-readobj --sections --section-data --macho-segment %t3 \
13# RUN:   | FileCheck %s --check-prefix=ONLY-TEXT-SECTION
14
15# ONLY-TEXT-SECTION:      Sections [
16# ONLY-TEXT-SECTION-NEXT:   Section {
17# ONLY-TEXT-SECTION-NEXT:     Index: 0
18# ONLY-TEXT-SECTION-NEXT:     Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
19# ONLY-TEXT-SECTION-NEXT:     Segment: __TEXT (5F 5F 54 45 58 54 00 00 00 00 00 00 00 00 00 00)
20# ONLY-TEXT-SECTION-NEXT:     Address: 0x0
21# ONLY-TEXT-SECTION-NEXT:     Size: 0x4
22# ONLY-TEXT-SECTION-NEXT:     Offset: 184
23# ONLY-TEXT-SECTION-NEXT:     Alignment: 0
24# ONLY-TEXT-SECTION-NEXT:     RelocationOffset: 0x0
25# ONLY-TEXT-SECTION-NEXT:     RelocationCount: 0
26# ONLY-TEXT-SECTION-NEXT:     Type: Regular (0x0)
27# ONLY-TEXT-SECTION-NEXT:     Attributes [ (0x800004)
28# ONLY-TEXT-SECTION-NEXT:       PureInstructions (0x800000)
29# ONLY-TEXT-SECTION-NEXT:       SomeInstructions (0x4)
30# ONLY-TEXT-SECTION-NEXT:     ]
31# ONLY-TEXT-SECTION-NEXT:     Reserved1: 0x0
32# ONLY-TEXT-SECTION-NEXT:     Reserved2: 0x0
33# ONLY-TEXT-SECTION-NEXT:     Reserved3: 0x0
34# ONLY-TEXT-SECTION-NEXT:     SectionData (
35# ONLY-TEXT-SECTION-NEXT:       0000: AABBCCDD                             |....|
36# ONLY-TEXT-SECTION-NEXT:     )
37# ONLY-TEXT-SECTION-NEXT:   }
38# ONLY-TEXT-SECTION-NEXT: ]
39# ONLY-TEXT-SECTION-NEXT: Segment {
40# ONLY-TEXT-SECTION-NEXT:   Cmd: LC_SEGMENT_64
41# ONLY-TEXT-SECTION-NEXT:   Name:
42# ONLY-TEXT-SECTION-NEXT:   Size: 152
43# ONLY-TEXT-SECTION-NEXT:   vmaddr: 0x0
44# ONLY-TEXT-SECTION-NEXT:   vmsize: 0x4
45# ONLY-TEXT-SECTION-NEXT:   fileoff: 184
46# ONLY-TEXT-SECTION-NEXT:   filesize: 4
47# ONLY-TEXT-SECTION-NEXT:   maxprot: rwx
48# ONLY-TEXT-SECTION-NEXT:   initprot: rwx
49# ONLY-TEXT-SECTION-NEXT:   nsects: 1
50# ONLY-TEXT-SECTION-NEXT:   flags: 0x0
51# ONLY-TEXT-SECTION-NEXT: }
52
53## Remove all sections if the specified section name is not present in the input.
54# RUN: llvm-objcopy --only-section __TEXT,__nonexistent %t %t4 2>&1
55# RUN: llvm-readobj --sections --section-data --macho-segment %t4 \
56# RUN:   | FileCheck %s --check-prefix=NONEXISTENT-SECTION
57
58# NONEXISTENT-SECTION:      Sections [
59# NONEXISTENT-SECTION-NEXT: ]
60# NONEXISTENT-SECTION-NEXT: Segment {
61# NONEXISTENT-SECTION-NEXT:   Cmd: LC_SEGMENT_64
62# NONEXISTENT-SECTION-NEXT:   Name:
63# NONEXISTENT-SECTION-NEXT:   Size: 72
64# NONEXISTENT-SECTION-NEXT:   vmaddr: 0x0
65# NONEXISTENT-SECTION-NEXT:   vmsize: 0x0
66# NONEXISTENT-SECTION-NEXT:   fileoff: 104
67# NONEXISTENT-SECTION-NEXT:   filesize: 0
68# NONEXISTENT-SECTION-NEXT:   maxprot: rwx
69# NONEXISTENT-SECTION-NEXT:   initprot: rwx
70# NONEXISTENT-SECTION-NEXT:   nsects: 0
71# NONEXISTENT-SECTION-NEXT:   flags: 0x0
72# NONEXISTENT-SECTION-NEXT: }
73
74## Use wildcard to specify all sections under the __TEXT segment.
75# RUN: llvm-objcopy --only-section "__TEXT,*" %t %t5 2>&1
76# RUN: llvm-readobj --file-header --sections %t5 \
77# RUN:   | FileCheck %s --check-prefix=WILDCARD
78
79## Make sure that it doesn't care about the segment/section name separator ",".
80# RUN: llvm-objcopy --only-section "__TEXT*" %t %t6 2>&1
81# RUN: cmp %t5 %t6
82
83# WILDCARD:      NumOfLoadCommands: 1
84# WILDCARD:      Sections [
85# WILDCARD:        Index: 0
86# WILDCARD-NEXT:   Name: __text (5F 5F 74 65 78 74 00 00 00 00 00 00 00 00 00 00)
87# WILDCARD:        Index: 1
88# WILDCARD-NEXT:   Name: __const (5F 5F 63 6F 6E 73 74 00 00 00 00 00 00 00 00 00)
89# WILDCARD-NOT:    Index: 2
90# WILDCARD:      ]
91
92--- !mach-o
93FileHeader:
94  magic:           0xFEEDFACF
95  cputype:         0x01000007
96  cpusubtype:      0x00000003
97  filetype:        0x00000001
98  ncmds:           1
99  sizeofcmds:      312
100  flags:           0x00002000
101  reserved:        0x00000000
102LoadCommands:
103  - cmd:             LC_SEGMENT_64
104    cmdsize:         312
105    segname:         ''
106    vmaddr:          0
107    vmsize:          12
108    fileoff:         344
109    filesize:        12
110    maxprot:         7
111    initprot:        7
112    nsects:          3
113    flags:           0
114    Sections:
115      - sectname:        __text
116        segname:         __TEXT
117        addr:            0x0000000000000000
118        content:         'AABBCCDD'
119        size:            4
120        offset:          344
121        align:           0
122        reloff:          0x00000000
123        nreloc:          0
124        flags:           0x80000400
125        reserved1:       0x00000000
126        reserved2:       0x00000000
127        reserved3:       0x00000000
128      - sectname:        __data
129        segname:         __DATA
130        addr:            0x0000000000000004
131        content:         'DDAADDAA'
132        size:            4
133        offset:          348
134        align:           0
135        reloff:          0x00000000
136        nreloc:          0
137        flags:           0x00000000
138        reserved1:       0x00000000
139        reserved2:       0x00000000
140        reserved3:       0x00000000
141      - sectname:        __const
142        segname:         __TEXT
143        addr:            0x0000000000000008
144        content:         'EEFFEEFF'
145        size:            4
146        offset:          352
147        align:           0
148        reloff:          0x00000000
149        nreloc:          0
150        flags:           0x00000000
151        reserved1:       0x00000000
152        reserved2:       0x00000000
153        reserved3:       0x00000000
154