Skip to content

Commit 52fa04f

Browse files
authored
Fix extract_ir.py for macos. (#260)
* Fix extract_ir.py for macos. For whatever reason, the section names for .llvmcmd and .llvmbc are different on macos. One problem is that macos uses macho object files instead of elf, so in addition to the section names there are segment names that need to be specified. The other problem is that regardless of the segments, the section names are different. This patch makes the segment name configurable by a flag, and gives sensible defaults for linux + documentation for macos. * Remove spurious changes. * OS -> file format.
1 parent 91a2f27 commit 52fa04f

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

compiler_opt/tools/extract_ir.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,18 @@
7676
'passed in the local case.')
7777
flags.mark_flags_as_required(['output_dir'])
7878

79+
flags.DEFINE_string(
80+
'cmd_section_name', '.llvmcmd',
81+
'The section name passed to llvm-objcopy. For ELF object files, the '
82+
'default .llvmcmd is correct. For Mach-O object files, one should use '
83+
'something like __LLVM,__cmdline')
84+
85+
flags.DEFINE_string(
86+
'bitcode_section_name', '.llvmbc',
87+
'The section name passed to llvm-objcopy. For ELF object files, the '
88+
'default .llvmbc is correct. For Mach-O object files, one should use '
89+
'__LLVM,__bitcode')
90+
7991
FLAGS = flags.FLAGS
8092

8193

@@ -98,10 +110,7 @@ def get_thinlto_index(cmdline: str, basedir: str) -> Optional[str]:
98110

99111

100112
class TrainingIRExtractor:
101-
"""IR and command line extraction from an object file.
102-
103-
The object file is assumed to have the .llvmbc and .llvmcmd sections.
104-
"""
113+
"""IR and command line extraction from an object file."""
105114

106115
def __init__(self, obj_relative_path, output_base_dir, obj_base_dir=None):
107116
"""Set up a TrainingIRExtractor.
@@ -156,16 +165,18 @@ def thinlto_index_file(self):
156165
return os.path.join(self.dest_dir(), self.module_name() + '.thinlto.bc')
157166

158167
def _get_extraction_cmd_command(self, llvm_objcopy_path):
159-
"""Call llvm_objcopy to extract the .llvmcmd section in self._cmd_file."""
168+
"""Call llvm_objcopy to extract the llvmcmd section in self._cmd_file."""
160169
return [
161-
llvm_objcopy_path, '--dump-section=.llvmcmd=' + self.cmd_file(),
170+
llvm_objcopy_path,
171+
'--dump-section=' + FLAGS.cmd_section_name + '=' + self.cmd_file(),
162172
self.input_obj(), '/dev/null'
163173
]
164174

165175
def _get_extraction_bc_command(self, llvm_objcopy_path):
166-
"""Call llvm_objcopy to extract the .llvmbc section in self._bc_file."""
176+
"""Call llvm_objcopy to extract the llvmbc section in self._bc_file."""
167177
return [
168-
llvm_objcopy_path, '--dump-section=.llvmbc=' + self.bc_file(),
178+
llvm_objcopy_path,
179+
'--dump-section=' + FLAGS.bitcode_section_name + '=' + self.bc_file(),
169180
self.input_obj(), '/dev/null'
170181
]
171182

@@ -371,4 +382,5 @@ def main(argv):
371382

372383

373384
if __name__ == '__main__':
385+
multiprocessing.set_start_method('fork')
374386
app.run(main)

0 commit comments

Comments
 (0)