Skip to content

Commit 1f2df9c

Browse files
committed
2 parents 2cab90c + 48461df commit 1f2df9c

File tree

5 files changed

+312
-73
lines changed

5 files changed

+312
-73
lines changed

NOTES.txt

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
WARNING:Did not recognize the compiler flag "-m32"
2+
WARNING:Did not recognize the compiler flag "-march=i686"
3+
WARNING:Did not recognize the compiler flag "-B/home/iam/world/usr/src/lib32/usr/lib32"
4+
WARNING:Did not recognize the compiler flag "-mpreferred-stack-boundary=2"
5+
WARNING:Did not recognize the compiler flag "-mno-align-long-strings"
6+
WARNING:Did not recognize the compiler flag "-mrtd"
7+
WARNING:Did not recognize the compiler flag "-mregparm=3"
8+
WARNING:Did not recognize the compiler flag "-mstack-alignment=8"
9+
WARNING:Did not recognize the compiler flag "-mllvm"
10+
WARNING:Did not recognize the compiler flag "-inline-threshold=3"
11+
WARNING:Did not recognize the compiler flag "-enable-load-pre=false"
12+
WARNING:Did not recognize the compiler flag "-mllvm"
13+
WARNING:Did not recognize the compiler flag "-simplifycfg-dup-ret"
14+
WARNING:Did not recognize the compiler flag "-march=i386"
15+
WARNING:Did not recognize the compiler flag "cat.lo" ....
16+
WARNING:Did not recognize the compiler flag "-Ttext"
17+
WARNING:Did not recognize the compiler flag "0x600"
18+
WARNING:Did not recognize the compiler flag "-m"
19+
WARNING:Did not recognize the compiler flag "elf_i386_fbsd"
20+
21+
22+
Darwin linker flags:
23+
24+
WARNING:Did not recognize the compiler flag "-dynamiclib"
25+
WARNING:Did not recognize the compiler flag "-current_version"
26+
WARNING:Did not recognize the compiler flag "2.2.2"
27+
WARNING:Did not recognize the compiler flag "-compatibility_version"
28+
WARNING:Did not recognize the compiler flag "2.2.0"
29+
30+
Attempt to attach bitcode stuff on MacOS using ld:
31+
32+
ld -r <file.o> -sectcreate __LLVM __llvm_bc <tmpfile> -o <file.o>
33+
34+
This works (it adds a new section called __llvm_bc in a new __LLVM
35+
segment), but the 'ld -r' part may have bad side effects. For example
36+
if the <file.o> is produced with -fvisibility=hidden then calling
37+
ld -r <file.o> converts the extern symbols into local symbols.
38+
39+
Workaround for now: give a warning if we see -fvisibility=hidden on
40+
the Mac.
41+
42+
43+
44+
TODO: extract-bc for MacOS
45+
46+
To extract bitcode from binaries, objects, or library files on Darwin:
47+
48+
otool -X -s __LLVM __llvm_bc yices_sat | xxd -r
49+
50+
Explanations:
51+
52+
otool -s __LLVM __llvm_bc file extracts and display the section we care about
53+
54+
flag -X removes some useless headers we don't want
55+
56+
xxd -r converts the hexdump to ASCII list of file names
57+
58+

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,12 @@ Example building bitcode archive
8282
# Produces src/LinearMath/libLinearMath.bca
8383
extract-bc src/LinearMath/libLinearMath.a
8484

85+
Example building an Operating System
86+
================================
87+
88+
To see how to build freeBSD 10.0 from scratch check out the guide
89+
[here.](../master/README-freeBSD.md)
90+
8591
Debugging
8692
=========
8793

driver/utils.py

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
# This is the ELF section name inserted into binaries
3535
elfSectionName='.llvm_bc'
3636

37+
# These are the MACH_O segment and section name
38+
darwinSegmentName='__LLVM'
39+
darwinSectionName='__llvm_bc'
40+
41+
3742
# Internal logger
3843
_logger = logging.getLogger(__name__)
3944

@@ -182,6 +187,24 @@ def __init__(self, inputList, exactMatches={}, patternMatches={}):
182187
'-static' : (0, ArgumentListFilter.linkUnaryCallback),
183188
'-nostdlib' : (0, ArgumentListFilter.linkUnaryCallback),
184189
'-nodefaultlibs' : (0, ArgumentListFilter.linkUnaryCallback),
190+
# darwin flags
191+
'-dynamiclib' : (0, ArgumentListFilter.linkUnaryCallback),
192+
'-current_version' : (1, ArgumentListFilter.linkBinaryCallback),
193+
'-compatibility_version' : (1, ArgumentListFilter.linkBinaryCallback),
194+
195+
# bd: need to warn the darwin user that these flags will rain on their parade
196+
# (the Darwin ld is a bit single minded)
197+
#
198+
# 1) compilation with -fvisibility=hidden causes trouble when we try to
199+
# attach bitcode filenames to an object file. The global symbols in object
200+
# files get turned into local symbols when we invoke 'ld -r'
201+
#
202+
# 2) all stripping commands (e.g., -dead_strip) remove the __LLVM segment after
203+
# linking
204+
#
205+
'-fvisibility=hidden' : (0, ArgumentListFilter.darwinWarningCompileUnaryCallback),
206+
'-Wl,-dead_strip' : (0, ArgumentListFilter.darwinWarningLinkUnaryCallback),
207+
185208
}
186209

187210
#
@@ -310,6 +333,20 @@ def linkUnaryCallback(self, flag):
310333
def compileUnaryCallback(self, flag):
311334
self.compileArgs.append(flag)
312335

336+
def darwinWarningCompileUnaryCallback(self, flag):
337+
if sys.platform.startswith('darwin'):
338+
_logger.warning('The flag "{0}" cannot be used with this tool'.format(flag))
339+
sys.exit(1)
340+
else:
341+
self.compileArgs.append(flag)
342+
343+
def darwinWarningLinkUnaryCallback(self, flag):
344+
if sys.platform.startswith('darwin'):
345+
_logger.warning('The flag "{0}" cannot be used with this tool'.format(flag))
346+
sys.exit(1)
347+
else:
348+
self.linkArgs.append(flag)
349+
313350
def defaultBinaryCallback(self, flag, arg):
314351
_logger.warning('Ignoring compiler arg pair: "{0} {1}"'.format(flag, arg))
315352

@@ -395,19 +432,32 @@ def getFileType(cls, fileName):
395432
output = fileP.communicate()[0]
396433
output = output.decode()
397434
if 'ELF' in output and 'executable' in output:
398-
return cls.EXECUTABLE
435+
return cls.ELF_EXECUTABLE
436+
if 'Mach-O' in output and 'executable' in output:
437+
return cls.MACH_EXECUTABLE
399438
elif 'ELF' in output and 'shared' in output:
400-
return cls.SHARED
439+
return cls.ELF_SHARED
440+
elif 'Mach-O' in output and 'dynamically linked shared' in output:
441+
return cls.MACH_SHARED
401442
elif 'current ar archive' in output:
402443
return cls.ARCHIVE
403444
elif 'ELF' in output and 'relocatable' in output:
404-
return cls.OBJECT
445+
return cls.ELF_OBJECT
446+
elif 'Mach-O' in output and 'object' in output:
447+
return cls.MACH_OBJECT
405448
else:
406449
return cls.UNKNOWN
407450

408451
@classmethod
409452
def init(cls):
410-
for (index, name) in enumerate(('UNKNOWN', 'EXECUTABLE', 'OBJECT', 'ARCHIVE', 'SHARED')):
453+
for (index, name) in enumerate(('UNKNOWN',
454+
'ELF_EXECUTABLE',
455+
'ELF_OBJECT',
456+
'ELF_SHARED',
457+
'MACH_EXECUTABLE',
458+
'MACH_OBJECT',
459+
'MACH_SHARED',
460+
'ARCHIVE')):
411461
setattr(cls, name, index)
412462
cls.revMap[index] = name
413463

@@ -437,8 +487,12 @@ def attachBitcodePathToObject(bcPath, outFileName):
437487
os.fsync(f.fileno())
438488
f.close()
439489

440-
# Now write our .llvm_bc section
441-
objcopyCmd = ['objcopy', '--add-section', '{0}={1}'.format(elfSectionName, f.name), outFileName]
490+
491+
# Now write our bitcode section
492+
if (sys.platform.startswith('darwin')):
493+
objcopyCmd = ['ld', '-r', outFileName, '-sectcreate', darwinSegmentName, darwinSectionName, f.name, '-o', outFileName]
494+
else:
495+
objcopyCmd = ['objcopy', '--add-section', '{0}={1}'.format(elfSectionName, f.name), outFileName]
442496
orc = 0
443497

444498
try:
@@ -600,7 +654,7 @@ def buildAndAttachBitcode(builder):
600654
# "... -c -o foo.o" or even "... -c -o foo.So" which is OK, but we could also have
601655
# "... -c -o crazy-assed.objectfile" which we wouldn't get right (yet)
602656
# so we need to be careful with the objFile and bcFile
603-
# maybe python-magic is in out future ...
657+
# maybe python-magic is in our future ...
604658
srcFile = af.inputFiles[0]
605659
(objFile, bcFile) = af.getArtifactNames(srcFile, hidden)
606660
if af.outputFilename is not None:

0 commit comments

Comments
 (0)